Stream Method Example:
package java8stream;
public class Employee {
private String firstName;
private String lastName;
private String email;
private String gender;
private String newJoiner;
private int salary;
private int rating;
public Employee() {
super();
}
public Employee(String empId, String firstName, String lastName, String email, String gender, String newJoiner,
int salary, int rating) {
super();
this.empId = empId;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.gender = gender;
this.newJoiner = newJoiner;
this.salary = salary;
this.rating = rating;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getNewJoiner() {
return newJoiner;
}
public void setNewJoiner(String newJoiner) {
this.newJoiner = newJoiner;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getRating() {
return rating;
}
public void setRating(int rating) {
this.rating = rating;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email
+ ", gender=" + gender + ", newJoiner=" + newJoiner + ", salary=" + salary + ", rating=" + rating + "]";
}
}
===========================================
package java8stream;
import java.util.Arrays;
import java.util.List;
public class EmployeeList {
public static void main(String[] args) {
List<Employee> empList = getEmpList();
System.out.println(empList);
}
public static List<Employee> getEmpList(){
return Arrays.asList(
new Employee("59-385-1088","Govind","Khan","govindkhan@gmail.com","Male","TRUE",101146,0),
new Employee("73-274-6476","Ajit","Kumar","ajitkumar@gmail.com","Male","FALSE",29310,2),
new Employee("85-939-9584","Nitya","kumari","nityakumari@twitter.com","Female","FALSE",62291,4),
new Employee("08-180-8292","Saurabh","Sharma","saurabhsharma@twitter.com","Male","FALSE",142439,4),
new Employee("21-825-2623","Sachin","Raj","sachinraj@gmail.com","Male","FALSE",128764,5),
new Employee("66-708-5539","Nikee","kumari","nikeekumari@gmail.com","Female","FALSE",152924,4),
new Employee("81-697-2363","Rishabh","Verma","rishabhverma@twitter.com","Male","TRUE",128907,0),
new Employee("63-019-1110","Nihal","Shaikh","nihalshaikh@twitter.com","Male","TRUE",25100,0)
);
}
}
===========================================
package java8stream;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;
public class StreamOperation {
public static void main(String[] args) {
// We have a list of employees with empId, firstName, lastName, email,
// gender,newJoiner, salary and rating.
List<Employee> empList = EmployeeList.getEmpList();
// case :1 We will filter the employees list with gender as Female
empList.stream().filter(s -> s.getGender().equals("Female")).forEach(System.out::println);
// case :2 We will filter the employees list with newJoiner as True
empList.stream().filter(s -> s.getNewJoiner().equals("TRUE")).forEach(System.out::println);
// Case : 3 We will sort the employee list by rating asc.
empList.stream().sorted(Comparator.comparing(Employee::getRating)).forEach(System.out::println);
// case 4: We will sort the employee list by rating desc.
empList.stream().sorted(Comparator.comparing(Employee::getRating).reversed()).forEach(System.out::println);
// case:5 We will sort the employee list by both rating and salary
Comparator<Employee> sortByRating = (e1, e2) -> Double.compare(e1.getRating(), e2.getRating());
Comparator<Employee> sortBySalary = (e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary());
// Sort by Name then Sort by Salary
empList.stream().sorted(sortByRating.thenComparing(sortBySalary)).forEach(e -> System.out.println(e));
// case: 6 Match Method: We will see all employees with salary more than 1000
boolean isSalary = empList.stream().allMatch(s -> s.getSalary() > 1000);
System.out.println(isSalary);
// case :7 We will retrieve employee with max salary
empList.stream().max(Comparator.comparing(Employee::getSalary)).ifPresent(System.out::println);
// case :8 We will retrieve employee with max rating
empList.stream().max(Comparator.comparing(Employee::getRating)).ifPresent(System.out::println);
// case :9 We will retrieve employee with min salary
empList.stream().min(Comparator.comparing(Employee::getSalary)).ifPresent(System.out::println);
// case :10 We will retrieve employee with min rating
empList.stream().min(Comparator.comparing(Employee::getRating)).ifPresent(System.out::println);
// case :11 GroupBy Function : We will group all our employees by Gender
Map<String, List<Employee>> employeesBygender = empList.stream()
.collect(Collectors.groupingBy(Employee::getGender));
employeesBygender.forEach(((k, v) -> {
System.out.println(k);
v.forEach(System.out::println);
}));
}
}