Java Collectors: partitioningBy
November 29, 2014
Java 8 Collectors.partitioningBy is a method that partitions the element of stream always in two part. It returns a Collector that stores the values in a Map. The key of map can be only true and false.
Find the syntax of partitioningBy method. This method accepts a predicate and returns a Collector.
public static <T> Collector<T,?,Map<Boolean,List<T>>> partitioningBy(Predicate<? super T> predicate)
Student.java
package com.concretepage.util.stream; public class Student { private String name; private int age; public Student(String name,int age){ this.name=name; this.age=age; } public String getName() { return name; } public int getAge() { return age; } }
Map<Boolean, List<Student>> stdByClass = list.stream() .collect(Collectors.partitioningBy(s -> s.getAge() > 20));
CollectorsPartitioningBy.java
package com.concretepage.util.stream; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.stream.Collectors; public class CollectorsPartitioningBy { public static void main(String[] args) { Student s1 = new Student("Ram", 18); Student s2 = new Student("Shyam",22); Student s3 = new Student("Mohan",19); Student s4 = new Student("Mahesh",20); Student s5 = new Student("Krishna",21); List<Student> list = Arrays.asList(s1,s2,s3,s4,s5); //partition of Student on the basis of age System.out.println("----Partition of Student on the basis of age >20 ----"); Map<Boolean, List<Student>> stdByClass = list.stream() .collect(Collectors.partitioningBy(s -> s.getAge() > 20)); stdByClass.forEach((k,v)->System.out.println("Key:"+k+" "+ ((List<Student>)v).stream().map(s->s.getName()).collect(Collectors.joining(",")))); } }
----Partition of Student on the basis of age >20 ---- Key:false Ram,Mohan,Mahesh Key:true Shyam,Krishna