User Tools

Site Tools


programming:java:java8

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
programming:java:java8 [2020/03/26 16:58] – [Interfaces relevant for Lambdas] skipidarprogramming: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 code would produce the floowing output:+THe following code would produce the floowing output:
  
 **code**  **code** 
-<code+<sxh java
-dishes.stream().filter(a -> {System.out.println(a.name); return true;}).distinct().map(a -> {System.out.println(a.type); return a.name;}); +        dishes.stream().filter(a -> { 
-</code>+            System.out.println(a.name); 
 +            return true; 
 +        }).distinct().map(a -> { 
 +            System.out.println(a.type); 
 +            return a.name; 
 +        }); 
 +</sxh>
  
 **output**  **output** 
Line 319: Line 325:
  
  
-This means that steps during parallel execution - methods which can block the parallel execution should not be executed inside the Stream-pipeline.+This means that steps during parallel execution - methods that can block the parallel execution should not be executed inside the Stream-pipeline.
  
  
Line 372: Line 378:
 | reducing | The type produced by the reduction operation  | Reduce the stream to a single value starting from an initial value used as accumulator and iteratively combining it with each item of the stream using a BinaryOperator.  | <code> int totalCalories = menuStream.collect(reducing(0, Dish::getCalories, Integer::sum));  </code> | | reducing | The type produced by the reduction operation  | Reduce the stream to a single value starting from an initial value used as accumulator and iteratively combining it with each item of the stream using a BinaryOperator.  | <code> int totalCalories = menuStream.collect(reducing(0, Dish::getCalories, Integer::sum));  </code> |
 | collectingAndThen  | The type returned by the transforming function  |   | <code> int howManyDishes = menuStream.collect(collectingAndThen(toList(), List::size));   </code> | | collectingAndThen  | The type returned by the transforming function  |   | <code> int howManyDishes = menuStream.collect(collectingAndThen(toList(), List::size));   </code> |
-| groupingBy | Map<K, List<T>> | Maps the stream to groups (as hashmap keys)  | <code> Map<Dish.Type, List<Dish>> dishesByType = menuStream.collect(groupingBy(Dish::getType)); </code> |+| groupingBy | Map<K, List<T>> | Maps the stream to groups (as hashmap keys)  | <sxh java> Map<Dish.Type, List<Dish>> dishesByType = menuStream.collect(groupingBy(Dish::getType)); </sxh> |
 | partitioningBy | Map<Boolean, List<T>> |  | <code> Map<Boolean, List<Dish>> vegetarianDishes =menuStream.collect(partitioningBy(Dish::isVegetarian)); </code> | | partitioningBy | Map<Boolean, List<T>> |  | <code> Map<Boolean, List<Dish>> vegetarianDishes =menuStream.collect(partitioningBy(Dish::isVegetarian)); </code> |
  
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>+<sxh java>
 CompletableFuture<Integer> c = null; CompletableFuture<Integer> c = null;
 CompletableFuture<String> c2 = null; CompletableFuture<String> c2 = null;
Line 970: Line 976:
 <sxh java> <sxh java>
  
-DoubleFunction<String> doubleValue -> String.valueOf(doubleValue); // function returns what you define+Consumer<Double> c = aDouble -> System.out.println(aDouble);  // no output 
 + 
 +Function<Double, String> aDouble -> String.valueOf(f);  // function returns what you define 
 +DoubleFunction<String> d = doubleValue -> String.valueOf(doubleValue); // same as previous
  
 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
  
 +</sxh>
 +
 +
 +===== Terminal Methods =====
 +
 +== findAny / findFirst ==
 +
 +<sxh java>
 +
 +            List<Integer> list = List.of(1, 2, 3, 4, 9, 8, 7, 6, 1, 2, 3);
 +
 +            // gives maybe 7, 8 or 9, cause findAny doesnt respect order
 +            int res = list
 +                    .stream()
 +                    .parallel()
 +                    .filter(integer -> integer > 6)
 +                    .findAny()
 +                    .orElseThrow();
 +                    
 +            assertThat(res, anyOf(is(7), is(8), is(9)));
 +
 +
 +
 +            // 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, is(9));
 +
 +</sxh>
 +
 +
 +== reduce ==
 +
 +<sxh java>
 +        // cruel method to concat numbers, just to demonstrate reduce
 +        List<Integer> list = List.of(1, 2, 3, 4, 9, 8, 7, 6, 1, 2);
  
 +        Integer res = list
 +                .stream()
 +                .mapToInt(value -> value)
 +                .reduce(0,
 +                        (integer, integer2) -> {
 +                            return Integer.parseInt(String.format("%s%s", integer, integer2));
 +                        }
 +                );
 +        System.out.println(res); // 1234987612
  
 +        // better way would be with java8
 +        String res2 = list
 +                .stream()
 +                .map(String::valueOf)
 +                .collect(Collectors.joining());
 +        System.out.println(res2); // 1234987612
 </sxh> </sxh>
  
programming/java/java8.1585241917.txt.gz · Last modified: (external edit)