Java 8 collectors class

Collectors is a final class that extends the Object class which provides reduction operations, such as accumulating elements into collections, summarizing elements according to various criteria, grouping etc.

Java Stream Collectors groupingBy and counting example

package com.w3schools;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class Test{  
    public static void main(String[] args) {  
    	List names =
	          Arrays.asList("Jai", "Naren", "Nidhi",
	             "Nidhi", "Naren", "Nidhi");
	
	      Map map =
	      names.stream().collect(
	          Collectors.groupingBy(
	             Function.identity(), Collectors.counting()
	          )
	      );
	
	      System.out.println(map);
    }  
}  

Output

{Naren=2, Jai=1, Nidhi=3}

Java Stream Collectors example of fetching data as list

package com.w3schools;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

class Student{
	int rollNo;
	String name;
	int age;
	
	public Student(int rollNo, String name, int age){
		super();  
        this.rollNo = rollNo;  
        this.name = name; 
        this.age = age;
	}
}

public class TestExample {
	public static void main(String args[]){
	List list=new ArrayList();  
        
	//Adding Students   
        list.add(new Student(1,"Nidhi", 25));  
        list.add(new Student(3,"Parbhjot", 24));  
        list.add(new Student(2,"Amani", 25));  
        list.add(new Student(6,"Jai", 24));  
        list.add(new Student(7,"Mahesh", 26));  
        list.add(new Student(12,"Roxy", 25));
          
        //Fetching student names as List       
        List names = list.stream() 
                                     .map(n->n.name) 
                                     .collect(Collectors.toList());
        System.out.println(names);         	
     }
}

Output

[Nidhi, Parbhjot, Amani, Jai, Mahesh, Roxy]

Java Collectors collecting data as set example

package com.w3schools;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

class Student{
	int rollNo;
	String name;
	int age;
	
	public Student(int rollNo, String name, int age){
		super();  
        this.rollNo = rollNo;  
        this.name = name;  
        this.age = age;
	}
}

public class TestExample {
	public static void main(String args[]){
		List list=new ArrayList();  
        
		//Adding Students   
        list.add(new Student(1,"Nidhi", 25));  
        list.add(new Student(3,"Parbhjot", 24));  
        list.add(new Student(2,"Amani", 25));  
        list.add(new Student(6,"Jai", 24));  
        list.add(new Student(7,"Mahesh", 26));  
        list.add(new Student(12,"Roxy", 25));
          
       //Fetching student data as a Set       
        Set students = list.stream()
                             .filter(n-> n.rollNo>2)
                             .collect(Collectors.toSet());
        //Iterating Set       
        for(Student stu : students) { 
           System.out.println(stu.rollNo+" "+stu.name+" "+stu.age); 
        }         	
     }
}

Output

6 Jai 24
12 Roxy 25
3 Parbhjot 24
7 Mahesh 26
Please follow and like us:
Content Protection by DMCA.com