Wednesday 27 September 2017

GroupBy using Java 8 Stream | Java 8 streams tutorial | Java 8 streams | Streams in Java 8

public class Student
{
    private String studentName;
    private String teacherName;
    private int level;
    private String className;

    public Student(String studentName, String teacherName, int level,
            String className)
    {
        super();
        this.studentName = studentName;
        this.teacherName = teacherName;
        this.level = level;
        this.className = className;
    }

    public String getStudentName()
    {
        return studentName;
    }

    public void setStudentName(String studentName)
    {
        this.studentName = studentName;
    }

    public String getTeacherName()
    {
        return teacherName;
    }

    public void setTeacherName(String teacherName)
    {
        this.teacherName = teacherName;
    }

    public int getLevel()
    {
        return level;
    }

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

    public String getClassName()
    {
        return className;
    }

    public void setClassName(String className)
    {
        this.className = className;
    }

    @Override
    public String toString()
    {
        return "Student [studentName=" + studentName + ", teacherName="
                + teacherName + ", level=" + level + ", className=" + className
                + "]";
    }

}
StreamGroupByDemo1.java
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Group by teacher name:
 * 
 * SELECT column_name, count(column_name) 
 * FROM table 
 * GROUP BY column_name;
 * 
 * This example will show how to "group classes by a teacher's name". Here you
 * will pass the groupingBy method a function in the form of a method reference
 * extracting each teacher name to the corresponding Student which will return 1
 * key to many elements. This is similar to guava's Multimap collection which
 * allows for easy mapping of a single key to multiple values.
 *
 */
public class StreamGroupByDemo1
{
    public static void main(String[] args)
    {
        List<Student> studentList = getStudentList();

        Map<String, List<Student>> groupByTeachersMap = studentList.stream()
                .collect(Collectors.groupingBy(Student::getTeacherName));

        for (Map.Entry<String, List<Student>> entry : groupByTeachersMap
                .entrySet())
        {
            System.out.println("Teacher Name : " + entry.getKey());

            List<Student> list = entry.getValue();
            for (Student student : list)
            {
                System.out.println(student);
            }
            System.out.println("-------------------------------------");
        }

    }

    private static List<Student> getStudentList()
    {
        List<Student> studentList = new ArrayList<Student>();
        studentList.add(new Student("Peter", "Mr.John", 1, "Java Basics"));
        studentList.add(new Student("Ram", "Mr.John", 1, "Webservice Basics"));
        studentList.add(new Student("Juli", "Mr.John", 2, "Advance Java"));
        studentList.add(new Student("Dave", "Mr.Kumar", 1, "Ruby basics"));
        studentList.add(new Student("Raja", "Mr.Kumar", 2, "Advance Ruby"));
        return studentList;
    }

}
Output
Teacher Name : Mr.John
Student [studentName=Peter, teacherName=Mr.John, level=1, className=Java Basics]
Student [studentName=Ram, teacherName=Mr.John, level=1, className=Webservice Basics]
Student [studentName=Juli, teacherName=Mr.John, level=2, className=Advance Java]
-------------------------------------
Teacher Name : Mr.Kumar
Student [studentName=Dave, teacherName=Mr.Kumar, level=1, className=Ruby basics]
Student [studentName=Raja, teacherName=Mr.Kumar, level=2, className=Advance Ruby]
-------------------------------------

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

/**
 * Group by class level:
 * 
 * SELECT column_name, count(column_name) 
 * FROM table 
 * GROUP BY column_name; 
 * 
 * This example will show "show all classes by level".
 *
 */
public class StreamGroupByDemo2
{
    public static void main(String[] args)
    {
        List<Student> studentList = getStudentList();

        Map<Integer, List<Student>> groupByLevelMap = studentList.stream()
                .collect(Collectors.groupingBy(Student::getLevel));

        for (Map.Entry<Integer, List<Student>> entry : groupByLevelMap
                .entrySet())
        {
            System.out.println("Level : " + entry.getKey());

            List<Student> list = entry.getValue();
            for (Student student : list)
            {
                System.out.println(student);
            }
            System.out.println("----------------------------------");
        }

    }

    private static List<Student> getStudentList()
    {
        List<Student> studentList = new ArrayList<Student>();
        studentList.add(new Student("Peter", "Mr.John", 1, "Java Basics"));
        studentList.add(new Student("Ram", "Mr.John", 1, "Webservice Basics"));
        studentList.add(new Student("Juli", "Mr.John", 2, "Advance Java"));
        studentList.add(new Student("Dave", "Mr.Kumar", 1, "Ruby basics"));
        studentList.add(new Student("Raja", "Mr.Kumar", 2, "Advance Ruby"));
        return studentList;
    }

}
Output
Level : 1
Student [studentName=Peter, teacherName=Mr.John, level=1, className=Java Basics]
Student [studentName=Ram, teacherName=Mr.John, level=1, className=Webservice Basics]
Student [studentName=Dave, teacherName=Mr.Kumar, level=1, className=Ruby basics]
----------------------------------
Level : 2
Student [studentName=Juli, teacherName=Mr.John, level=2, className=Advance Java]
Student [studentName=Raja, teacherName=Mr.Kumar, level=2, className=Advance Ruby]
----------------------------------

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

/**
 * GroupBy aggregate:
 * 
 * SELECT LEVEL, count(LEVEL) 
 * FROM STUDENT 
 * GROUP BY LEVEL;
 * 
 * This example will "count the number of classes per level":
 * 
 */
public class StreamGroupByDemo3
{
    public static void main(String[] args)
    {
        List<Student> studentList = getStudentList();

        /*
         * In an overloaded groupingBy method, you can pass a second collector.
         * Collectors have various reduce operations which can be passed, in
         * this case Collectors.counting which will count the number of classes
         * in each level.
         */
        Map<Integer, Long> groupByLevelMap = studentList.stream()
                .collect(Collectors.groupingBy(Student::getLevel,Collectors.counting()));

        for (Map.Entry<Integer, Long> entry : groupByLevelMap.entrySet())
        {
            System.out.println("Level = " + entry.getKey() + ", Count = "
                    + entry.getValue());
        }

    }

    private static List<Student> getStudentList()
    {
        List<Student> studentList = new ArrayList<Student>();
        studentList.add(new Student("Peter", "Mr.John", 1, "Java Basics"));
        studentList.add(new Student("Ram", "Mr.John", 1, "Webservice Basics"));
        studentList.add(new Student("Juli", "Mr.John", 2, "Advance Java"));
        studentList.add(new Student("Dave", "Mr.Kumar", 1, "Ruby basics"));
        studentList.add(new Student("Raja", "Mr.Kumar", 2, "Advance Ruby"));
        return studentList;
    }

}
Output
Level = 1, Count = 3
Level = 2, Count = 2

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/StreamDemo_groupBy_level.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/StreamDemo_groupBy_level

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/4c6dd26ede26896338d54f8923608ecad05bf799/BasicJava/StreamDemo_groupBy_level/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment