Example usage for java.util.stream Collectors groupingBy

List of usage examples for java.util.stream Collectors groupingBy

Introduction

In this page you can find the example usage for java.util.stream Collectors groupingBy.

Prototype

public static <T, K, A, D> Collector<T, ?, Map<K, D>> groupingBy(Function<? super T, ? extends K> classifier,
        Collector<? super T, A, D> downstream) 

Source Link

Document

Returns a Collector implementing a cascaded "group by" operation on input elements of type T , grouping elements according to a classification function, and then performing a reduction operation on the values associated with a given key using the specified downstream Collector .

Usage

From source file:Main.java

public static void main(String... args) {
    Map<Type, Long> t = Food.menu.stream().collect(Collectors.groupingBy(Food::getType, Collectors.counting()));

    System.out.println(t);/*from   w  w  w  . j  a v a 2s.  co  m*/

}

From source file:Main.java

public static void main(String... args) {
    Map<Type, Integer> o = Food.menu.stream()
            .collect(Collectors.groupingBy(Food::getType, Collectors.summingInt(Food::getCalories)));

    System.out.println(o);//from   www . j av  a  2  s.c  o  m

}

From source file:Main.java

public static void main(String... args) {
    Map<Type, Food> o = Food.menu.stream().collect(Collectors.groupingBy(Food::getType,
            Collectors.collectingAndThen(
                    Collectors.reducing((Food d1, Food d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2),
                    Optional::get)));

    System.out.println(o);/*from   ww w  .jav  a  2 s.c  o m*/

}

From source file:Main.java

public static void main(String... args) {
    Map<Type, Optional<Food>> o = Food.menu.stream().collect(Collectors.groupingBy(Food::getType,
            Collectors.reducing((Food d1, Food d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2)));

    System.out.println(o);/*  w  w w. j av  a  2s.  c  o m*/

}

From source file:Main.java

public static void main(String... args) {
    Map<Type, Set<CaloricLevel>> o = Food.menu.stream()
            .collect(Collectors.groupingBy(Food::getType, Collectors.mapping(dish -> {
                if (dish.getCalories() <= 400)
                    return CaloricLevel.DIET;
                else if (dish.getCalories() <= 700)
                    return CaloricLevel.NORMAL;
                else
                    return CaloricLevel.FAT;
            }, Collectors.toSet())));

    System.out.println(o);// w  ww  . j  ava2 s.c  om

}

From source file:Main.java

public static void main(String[] args) {
    Map<Employee.Gender, String> namesByGender = Employee.persons().stream().collect(Collectors
            .groupingBy(Employee::getGender, Collectors.mapping(Employee::getName, Collectors.joining(", "))));
    System.out.println(namesByGender);
}

From source file:Main.java

public static void main(String[] args) {
    Map<Employee.Gender, List<String>> namesByGender = Employee.persons().stream().collect(Collectors
            .groupingBy(Employee::getGender, Collectors.mapping(Employee::getName, Collectors.toList())));

    System.out.println(namesByGender);
}

From source file:Main.java

public static void main(String[] args) {
    Map<Employee.Gender, Long> countByGender = Employee.persons().stream()
            .collect(Collectors.groupingBy(Employee::getGender, Collectors.counting()));
    System.out.println(countByGender);
}

From source file:Main.java

public static void main(String[] args) {
    Map personsByGenderAndDobMonth = Employee.persons().stream().collect(
            Collectors.groupingBy(Employee::getGender, Collectors.groupingBy(p -> p.getDob().getMonth(),
                    Collectors.mapping(Employee::getName, Collectors.joining(", ")))));

    System.out.println(personsByGenderAndDobMonth);
}

From source file:Main.java

public static void main(String[] args) {
    Map<Gender, Map<Object, String>> personsByGenderAndDobMonth = Employee.persons().stream().collect(
            Collectors.groupingBy(Employee::getGender, Collectors.groupingBy(p -> p.getDob().getMonth(),
                    Collectors.mapping(Employee::getName, Collectors.joining(", ")))));

    System.out.println(personsByGenderAndDobMonth);
}