Example usage for java.util.stream Collectors partitioningBy

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

Introduction

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

Prototype

public static <T, D, A> Collector<T, ?, Map<Boolean, D>> partitioningBy(Predicate<? super T> predicate,
        Collector<? super T, A, D> downstream) 

Source Link

Document

Returns a Collector which partitions the input elements according to a Predicate , reduces the values in each partition according to another Collector , and organizes them into a Map whose values are the result of the downstream reduction.

Usage

From source file:Main.java

public static void main(String... args) {
    Map<Boolean, Map<Type, List<Food>>> o = Food.menu.stream()
            .collect(Collectors.partitioningBy(Food::isVegetarian, Collectors.groupingBy(Food::getType)));

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

}

From source file:Main.java

public static void main(String... args) {
    Map<Boolean, Food> o = Food.menu.stream().collect(Collectors.partitioningBy(Food::isVegetarian, Collectors
            .collectingAndThen(Collectors.maxBy(Comparator.comparingInt(Food::getCalories)), Optional::get)));
    System.out.println(o);/*from  ww w  .ja va2 s . c  o m*/

}

From source file:Main.java

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