Searching...
Saturday, 28 November 2015

Java 8 Stream aggregate operations example

20:18

Java 8 Streams

Sequence of elements − A stream provides a set of elements of specific type in a sequential manner. A stream gets/computes elements on demand. It never stores the elements.
Aggregate operations − Stream supports aggregate operations like filter,map, limit, reduce, find, match, and so on.

Here is one simple example which is using aggregate operations

package com.vinod.test;
import java.util.ArrayList;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class JavaStreamExample {


public static void main(String[] args) {

Employee emp1 = new Employee("Vinod", "Admin", "CA", 33);
Employee emp2 = new Employee("Santhosh", "SD", "CA", 34);
Employee emp3 = new Employee("Anish", "Fin", "CA", 30);
Employee emp4 = new Employee("Raghav", "Sales", "CA", 12);
Employee emp5 = new Employee("Raghav", "Sales", "CA", 12);

List<Employee> empList = new ArrayList<Employee>();
empList.add(emp1);
empList.add(emp2);
empList.add(emp3);
empList.add(emp4);
empList.add(emp5);

System.out.println("Before java 8");
for (Employee emp : empList) {
System.out.println(emp);
}
// java 8
System.out.println("Java 8 Iterating List");

empList.stream().forEach(System.out::println);



// Filter
System.out.println("Java 8 filtering a list");
List<Employee> filterEmployee = empList.stream()
.filter(e -> e.getName().equalsIgnoreCase("Vinod"))
.collect(Collectors.toList());;
System.out.println(filterEmployee.toString());



// Limit
List<Employee> limitEmployee = empList.stream().limit(2)
.collect(Collectors.toList());;
System.out.println(limitEmployee);

//Match
boolean matchEmployee = empList.stream().anyMatch(e->e.getAge()<30);
System.out.println("Match :"+matchEmployee);

//Map example
IntSummaryStatistics stats = empList.stream()
.mapToInt((x) -> x.getAge()).summaryStatistics();

System.out.println("Highest age in List : " + stats.getMax());
System.out.println("Lowest age in List : " + stats.getMin());
System.out.println("Average of all ages : " + stats.getAverage());

}

}

Output

Before java 8

Employee [name=Vinod, department=Admin, address=CA, age=33]

Employee [name=Santhosh, department=SD, address=CA, age=34]

Employee [name=Anish, department=Fin, address=CA, age=30]

Employee [name=Raghav, department=Sales, address=CA, age=12]

Employee [name=Raghav, department=Sales, address=CA, age=12]

Java 8 Iterating List

Employee [name=Vinod, department=Admin, address=CA, age=33]

Employee [name=Santhosh, department=SD, address=CA, age=34]

Employee [name=Anish, department=Fin, address=CA, age=30]

Employee [name=Raghav, department=Sales, address=CA, age=12]

Employee [name=Raghav, department=Sales, address=CA, age=12]

Java 8 filtering a list

[Employee [name=Vinod, department=Admin, address=CA, age=33]]

[Employee [name=Vinod, department=Admin, address=CA, age=33], Employee [name=Santhosh, department=SD, address=CA, age=34]]

Match  :true

Highest age in List : 34

Lowest age in List : 12

Average of all ages : 24.2


0 comments: