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 [2023/10/31 13:18] skipidarprogramming:java:java8 [2023/11/01 07:31] (current) – ↷ Page moved from camunda:programming:java:java8 to programming:java:java8 skipidar
Line 378: 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 990: Line 990:
 ===== Terminal Methods ===== ===== Terminal Methods =====
  
-== findAny / findFirst+== findAny / findFirst ==
  
 <sxh java> <sxh java>
Line 1021: Line 1021:
  
  
 +== 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>
  
programming/java/java8.1698758294.txt.gz · Last modified: by skipidar