PartitioningBy examples in Java 8

This article shows how to use Collectors#partitioningBy in Java 8 to partition students survey List report

1. Students survey class

StudentSurvey.java (POJO class)

package com.dzone.java8;
public class StudentSurvey{
    private String fullName;
    private int level;
    private double overallScore;

    public StudentSurvey(String fullName, int level, double overallPerformance){
        this.fullName = fullName;
        this.level = level;
        this.overallScore = overallPerformance;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public double getOverallScore() {
        return overallScore;
    }

    public void setOverallScore(double overallScore) {
        this.overallScore = overallScore;
    }

    @Override
    public String toString(){
        return String.format("Student survey report -> " + "Fullname : "+ fullName + ", Level : "+level
                + ", Overall score : "+overallScore +"\n");
    }

2. Partition the students survey report

Example to partition the survey report by using Collectors.partitioningBy to know the students whose overall score is less than 3.00

TestStudetSurvey.java

package com.dzone.java8;
import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

public class TestStudetSurvey {
    public static void main(String...args){
        List listSurvey = Arrays.asList(
                new StudentSurvey("Amuda Adeolu",5,4.789),
                new StudentSurvey("Adeola Amuda",3,3.567),
                new StudentSurvey("Dayo Amuda",2,2.345),
                new StudentSurvey("Unknown unknown",0,0),
                new StudentSurvey("New student",1,1));


        Map<boolean,list> surveyReport = listSurvey.stream()
                .collect(Collectors.partitioningBy( students -> students.getOverallScore() < 3.00 ));

        System.out.println("Students with overall score less than 3.000"+surveyReport.get(true)
                .stream().map( report -> report.getFullName())
                 .collect(Collectors.toSet()));

        System.out.println("Students with overall score greater than 3.000"+surveyReport.get(false)
                .stream().map( report -> report.getFullName())
                .collect(Collectors.toSet()));

    }

}

</boolean,list

Output


Students with overall score less than 3.000[New student, Dayo Amuda, Unknown unknown]
Students with overall score greater than 3.000[Amuda Adeolu, Adeola Amuda]

3. More Collectors#partitioningBy Examples

3.1 Counting the students survey report < 3.000


	
	System.out.println("Total no. of students with overall score less than 3.000 : "+surveyReport.get(true)
                .stream().map( report -> report.getFullName())
                .collect(Collectors.counting()));
     

3.2 Counting the students survey report > 3.000


	
	System.out.println("Total no. of students with overall score greater than 3.000 : "+surveyReport.get(true)
                .stream().map( report -> report.getFullName())
                .collect(Collectors.counting()));		

Output


Total no. of students with overall score less than 3.000 : 3

Total no. of students with overall score greater than 3.000 : 3

3.3 Partition of even and odd numbers within 1 – 10


	
package com.dzone.java8;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Example3 {
    public static void main(String...args){

        Map<boolean, list<long="">> result = IntStream.range(1, 10)
                .mapToObj(Long::new)
                .collect(Collectors.partitioningBy(
                        i -> i % 2 == 0));
        System.out.println(result);
    }

}

</boolean,>

{false=[1, 3, 5, 7, 9], true=[2, 4, 6, 8]}

Reference

  1. Collectors#partitioningBy
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: