programming:java:java8
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| programming:java:java8 [2017/06/14 13:49] – ↷ Page moved from java:java8 to programming:java:java8 skipidar | programming:java:java8 [2023/11/01 07:31] (current) – ↷ Page moved from camunda:programming:java:java8 to programming:java:java8 skipidar | ||
|---|---|---|---|
| Line 301: | Line 301: | ||
| - | THe floowing | + | THe following |
| **code** | **code** | ||
| - | <code> | + | <sxh java> |
| - | dishes.stream().filter(a -> {System.out.println(a.name); | + | dishes.stream().filter(a -> { |
| - | </code> | + | |
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | </sxh> | ||
| **output** | **output** | ||
| Line 319: | Line 325: | ||
| - | This means that steps during parallel execution - methods | + | This means that steps during parallel execution - methods |
| Line 372: | Line 378: | ||
| | reducing | The type produced by the reduction operation | | reducing | The type produced by the reduction operation | ||
| | collectingAndThen | | collectingAndThen | ||
| - | | groupingBy | Map<K, List< | + | | groupingBy | Map<K, List< |
| | partitioningBy | Map< | | partitioningBy | Map< | ||
| Line 558: | Line 564: | ||
| to execute some code when the future has completed the computation. | to execute some code when the future has completed the computation. | ||
| - | <sxh> | + | < |
| CompletableFuture< | CompletableFuture< | ||
| CompletableFuture< | CompletableFuture< | ||
| Line 958: | Line 964: | ||
| </ | </ | ||
| + | |||
| + | |||
| + | ===== Interfaces relevant for Lambdas ===== | ||
| + | |||
| + | If you would like to create a method accepting a lamda use the Argument, typed with one of those methods: | ||
| + | **java.util.function.IntBinaryOperator.** | ||
| + | |||
| + | |||
| + | Examples: | ||
| + | |||
| + | <sxh java> | ||
| + | |||
| + | Consumer< | ||
| + | |||
| + | Function< | ||
| + | DoubleFunction< | ||
| + | |||
| + | IntBinaryOperator plusOperation = (a, b) -> a + b; // operator on two int numbers a and b | ||
| + | |||
| + | LongPredicate l = longValue -> longValue < 0l; // boolean function | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Terminal Methods ===== | ||
| + | |||
| + | == findAny / findFirst == | ||
| + | |||
| + | <sxh java> | ||
| + | |||
| + | List< | ||
| + | |||
| + | // gives maybe 7, 8 or 9, cause findAny doesnt respect order | ||
| + | int res = list | ||
| + | .stream() | ||
| + | .parallel() | ||
| + | .filter(integer -> integer > 6) | ||
| + | .findAny() | ||
| + | .orElseThrow(); | ||
| + | | ||
| + | assertThat(res, | ||
| + | |||
| + | |||
| + | |||
| + | // gives 9, cause its the first in the row match to filter | ||
| + | int res2 = list | ||
| + | .stream() | ||
| + | .parallel() | ||
| + | .filter(integer -> integer > 6) | ||
| + | .findFirst() | ||
| + | .orElseThrow(); | ||
| + | |||
| + | assertThat(res, | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | == reduce == | ||
| + | |||
| + | <sxh java> | ||
| + | // cruel method to concat numbers, just to demonstrate reduce | ||
| + | List< | ||
| + | |||
| + | Integer res = list | ||
| + | .stream() | ||
| + | .mapToInt(value -> value) | ||
| + | .reduce(0, | ||
| + | (integer, integer2) -> { | ||
| + | return Integer.parseInt(String.format(" | ||
| + | } | ||
| + | ); | ||
| + | System.out.println(res); | ||
| + | |||
| + | // better way would be with java8 | ||
| + | String res2 = list | ||
| + | .stream() | ||
| + | .map(String:: | ||
| + | .collect(Collectors.joining()); | ||
| + | System.out.println(res2); | ||
| + | </ | ||
| + | |||
programming/java/java8.1497448193.txt.gz · Last modified: (external edit)
