CodesJava

Easy learning with example program codes

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.codesjava;
 
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<String> names =
	          Arrays.asList("Jai", "Naren", "Nidhi",
	             "Nidhi", "Naren", "Nidhi");
 
	      Map<String, Long> 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.codesjava;
 
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<Student> list=new ArrayList<Student>();  
 
	//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<String> 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.codesjava;
 
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<Student> list=new ArrayList<Student>();  
 
		//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<Student> 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
Sign Up/ Sign In
Ask a Question


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki