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 [2020/03/26 16:58] – [Interfaces relevant for Lambdas] 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 970: | Line 976: | ||
| <sxh java> | <sxh java> | ||
| - | DoubleFunction< | + | Consumer< |
| + | |||
| + | Function<Double, | ||
| + | DoubleFunction< | ||
| IntBinaryOperator plusOperation = (a, b) -> a + b; // operator on two int numbers a and b | IntBinaryOperator plusOperation = (a, b) -> a + b; // operator on two int numbers a and b | ||
| Line 976: | Line 985: | ||
| LongPredicate l = longValue -> longValue < 0l; // boolean function | 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.1585241917.txt.gz · Last modified: (external edit)
