Example 1: collect to List using Collectors filtering

Java 8 Collectors.filtering to similar to steam.filter but using collect() and Collectors.filtering() we can also apply filter and direct result to List,Set or any other collection. Collectors.filtering() require two arguments:

  1. First argument is Predicate
  2. Second argument is Collection
java.util.List<Employee> employees = Employee.getEmployee();
        employees.stream().collect(Collectors.filtering(employee -> {
            return employee.getGender().equals("Male");
        }, Collectors.toList()));

        employees.forEach(employee -> {
            System.out.println(employee);
        });

 Output:

1 : Bob : Male : Developer
2 : Joy : Male : Sr. Developer
3 : John : Male : CEO
4 : Bat : Male : Developer
5 : Jolly : Female : Developer
6 : Bobby : Female : Developer

 Example 2: collect to List using Collectors mapping

Using Collectors.mapping() its similar to Collectors.map() but using Collectors.mapping() we can direct store result to Collections like List, Set, Map or any other collections. In this example fetch employee name from Employee object and convert those names to List.

java.util.List<Employee> employees = Employee.getEmployee();
        java.util.List<String> employeeNames = employees.stream()
                                                        .collect(Collectors
                                                                .mapping(employee -> employee.getName(), Collectors
                                                                        .toList()));
        employeeNames.forEach(name -> {
            System.out.println(name);
        });

 Output:

Bob
Joy
John
Bat
Jolly
Bobby

Example 3: collect to Map using Collectors toMap

Java 8 : Collectors.toMap() is used to convert data to Map. In this example we are creating map of EmployeeNo and EmployeeName. Collectors.toMap() have two parameter

  1. Key Function
  2. Value Funcation
java.util.List<Employee> employees = Employee.getEmployee();
        java.util.Map<Integer, String> employeeNoAndNameMap = employees.stream().collect(Collectors
                .toMap(employee -> employee.getNo(), employee -> employee.getName()));

        employeeNoAndNameMap.forEach((no, name) -> {
            System.out.println("Employee No:" + no + " | Employee Name:" + name);
        });

Output:

Employee No:1 | Employee Name:Bob
Employee No:2 | Employee Name:Joy
Employee No:3 | Employee Name:John
Employee No:4 | Employee Name:Bat
Employee No:5 | Employee Name:Jolly
Employee No:6 | Employee Name:Bobby

 

Example 3: collect to Map using Collectors partitioningBy

Using Collectors.partitioningBy() method data can be divided in partitions. Collectors.partitioningBy() accept Predicate function which return true or false based on that two partition will Map will be created.Map contains <Boolean,List<T>>.   

java.util.List<Employee> employees = Employee.getEmployee();

        java.util.Map<Boolean, java.util.List<Employee>> genderMap = employees.stream()
                                                                              .collect(Collectors
                                                                                      .partitioningBy(employee -> {
                                                                                          return employee.getGender()
                                                                                                         .equals("Male");
                                                                                      }));

        java.util.List<Employee> maleEmployee = genderMap.get(true);
        System.out.println("Male Employees");
        maleEmployee.forEach(employee -> {
            System.out.println(employee);
        });
        java.util.List<Employee> femaleEmployee = genderMap.get(false);
        System.out.println("Female Employees");
        femaleEmployee.forEach(employee -> {
            System.out.println(employee);
        });

 Output:

Male Employees
1 : Bob : Male : Developer
2 : Joy : Male : Sr. Developer
3 : John : Male : CEO
4 : Bat : Male : Developer
Female Employees
5 : Jolly : Female : Developer
6 : Bobby : Female : Developer

 Example 4: collect to Map using Collectors PartitioningBy with Collectors couting

java.util.List<Employee> employees = Employee.getEmployee();
       java.util.Map<Boolean, Long> genderCount = employees.stream().collect(Collectors
               .partitioningBy(employee -> employee.getGender().equals("Male"), Collectors.counting()));
       System.out.println("Total Number of Male:" + genderCount.get(true));
       System.out.println("Total Number of Female:" + genderCount.get(false));

 Output:

Total Number of Male:4
Total Number of Female:2

 Example 5: collect to LinkedList using Collectors toCollection

Collectors contains methods like Collectors.toList(), Collectors.toSet() but if requires to convert data in other Collection format at that Collectors.toCollection() method use. Here stream will be converted in LinkedList.

java.util.List<Employee> employees = Employee.getEmployee();
        java.util.List<Employee> linkedList = employees.stream().collect(Collectors.toCollection(LinkedList::new));
        linkedList.forEach(System.out::println);

 Output:

1 : Bob : Male : Developer
2 : Joy : Male : Sr. Developer
3 : John : Male : CEO
4 : Bat : Male : Developer
5 : Jolly : Female : Developer
6 : Bobby : Female : Developer

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No
Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *