Java 8 Collectors: groupingBy Example

Java 8 Collectors: groupingBy Example

By Arvind Rai,  November 29, 2014
groupingBy is a static method of java.util.stream.Collectors in java 8. groupingBy does the grouping of elements on the basis of any given key and returns a Collector. Find the method syntax.
<T,K> Collector<T,?,Map<K,List<T>>> groupingBy(Function<? super T,? extends K> classifier) 
For the example, we will create a Student class. 
Student.java
package com.concretepage.util.stream;
public class Student {
    private String name;
    private int age;
    private String className;
    public Student(String name,String className,int age){
        this.name=name;
        this.age=age;
        this.className = className;
    }
    public String getName() {
        return name;
    }
    public int getAge() {
        return age;
    }
    public String getClassName() {
        return className;
    }
} 
Now if we want to group students on the basis of className, we will do as below.
Map<String, List<Student>> stdByClass = list.stream()
                                    .collect(Collectors.groupingBy(Student::getClassName));
We have a list of Student class. Grouping is done on the basis of student class name. List is converted into stream of student object. Then call collect method of stream. groupingBy of Collectors class checks each element of stream and gets class name and then group it as list. Finally we get a map where key is the one by which grouping is done. Find the complete example. 
CollectorsGroupingBy.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 CollectorsGroupingBy {
    public static void main(String[] args) {
        Student s1 = new Student("Ram", "A", 20);
        Student s2 = new Student("Shyam", "B", 22);
        Student s3 = new Student("Mohan", "A", 22);
        Student s4 = new Student("Mahesh", "C", 20);
        Student s5 = new Student("Krishna", "B", 21);
        List<Student> list = Arrays.asList(s1,s2,s3,s4,s5);
        //Group Student on the basis of ClassName
        System.out.println("----Group Student on the basis of ClassName----");
        Map<String, List<Student>> stdByClass = list.stream()
                    .collect(Collectors.groupingBy(Student::getClassName));
        
        stdByClass.forEach((k,v)->System.out.println("Key:"+k+"  "+ 
                ((List<Student>)v).stream().map(m->m.getName()).collect(Collectors.joining(","))));
        
        //Group Student on the basis of age
        System.out.println("----Group Student on the basis of age----");
        Map<Integer, List<Student>> stdByAge = list.stream()
                    .collect(Collectors.groupingBy(Student::getAge));
        
        stdByAge.forEach((k,v)->System.out.println("Key:"+k+"  "+ 
                ((List<Student>)v).stream().map(m->m.getName()).collect(Collectors.joining(","))));
    }
} 
Find the output.
----Group Student on the basis of ClassName----
Key:A  Ram,Mohan
Key:B  Shyam,Krishna
Key:C  Mahesh
----Group Student on the basis of age----
Key:20  Ram,Mahesh
Key:21  Krishna
Key:22  Shyam,Mohan 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值