| 23 | } |
| 24 | } |
| 25 | public static void main(String[] args) { |
| 26 | |
| 27 | // If Optional is not empty, map() first extracts |
| 28 | // the contents which it then passes |
| 29 | // to the function: |
| 30 | |
| 31 | test("Add brackets", s -> "[" + s + "]"); |
| 32 | |
| 33 | test("Increment", s -> { |
| 34 | try { |
| 35 | return Integer.parseInt(s) + 1 + ""; |
| 36 | } catch(NumberFormatException e) { |
| 37 | return s; |
| 38 | } |
| 39 | }); |
| 40 | |
| 41 | test("Replace", s -> s.replace("2", "9")); |
| 42 | |
| 43 | test("Take last digit", s -> s.length() > 0 ? |
| 44 | s.charAt(s.length() - 1) + "" : s); |
| 45 | } |
| 46 | // After the function is finished, map() wraps the |
| 47 | // result in an Optional before returning it: |
| 48 | } |