Java8 streams
First create model class named Person having getter and setters.
public class Persons {
int age;
double salary;
String name;
String city;
public Persons() {
super();
// TODO Auto-generated constructor stub
}
public Persons(int age, double salary, String name, String city) {
super();
this.age = age;
this.salary = salary;
this.name = name;
this.city = city;
}
@Override
public String toString() {
return “Persons [age=” + age + “, salary=” + salary + “, name=” + name + “, city=” + city + “]”;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
}
Create another class named StreamsTest where we implement Java8 streams using list.
package com.java.streams;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import com.java.model.Persons;
public class StreamsTest {
public static void main(String[] args)
{
// Create main list named lp of Persons class object which we create above
List<Persons> lp=new ArrayList<Persons>();
// Create list named la of Integer
List<Integer> la=new ArrayList<Integer>();
// Create list named ls of Double
List<Double> ls=new ArrayList<Double>();
// Create list named ln of String
List<String> ln=new ArrayList<String>();
// Create multiple objects of persons and add each on main list lp
lp.add(new Persons(14,34543.90,”Adil”,”Mumbai”));
lp.add(new Persons(12,58567.62,”Naeem”,”Delhi”));
lp.add(new Persons(17,3257.35,”Qadir”,”Chennai”));
lp.add(new Persons(19,9786.16,”Zain”,”Mumbai”));
lp.add(new Persons(13,8174.83,”Hammad”,”Delhi”));
lp.add(new Persons(15,6384.12,”Saeed”,”Delhi”));
lp.add(new Persons(11,8613.23,”Uzair”,”Mumbai”));
lp.add(new Persons(16,2693.45,”Moiz”,”Mumbai”));
lp.add(new Persons(18,4467.76,”Khalid”,”Mumbai”));
// To print coplete list
lp.stream().forEach(System.out::println);
// To print list with limit of only 2 records
lp.stream().limit(2).forEach(System.out::println);
// To embed filter on list having age greater then 13
lp.stream().filter(p->p.getAge()>13).forEach(System.out::println);
// To embed filter on list having name is Moiz
lp.stream().filter(p->p.getName()==”Moiz”).forEach(System.out::println);
// To embed filter with two conditions having age greater then 12 and less then 16
lp.stream().filter(p->p.getAge()>12 & p.getAge()<16).forEach(System.out::println);
// To embed filter with two conditions having name is Moiz or Saad
lp.stream().filter(p->p.getName()==”Uzair” | p.getName()==”Saad”).forEach(System.out::println);
// To embed filter with three conditions having age greater then 12 and less then 16 and city is Mumbai
lp.stream().filter(p->(p.getAge()>12 & p.getAge()<16) & p.getCity()==”Mumbai”).forEach(System.out::println);
// To embed filter with four conditions having salary greater then 300 and salary less then 600 and city is Mumbai or Chennai and print only name
lp.stream().filter(p->(p.getSalary()>3000.00 & p.getSalary()<6000.00) & p.getCity()==”Mumbai” | p.getCity()==”Chennai”).forEach(p->System.out.println(p.getName()));
// To embed filter with conditions having name start with A
lp.stream().filter(p->p.getName().startsWith(“A”)).forEach(System.out::println);
// To embed filter with conditions having name end with r
lp.stream().filter(p->p.getName().endsWith(“r”)).forEach(System.out::println);
// To map list on another list la which we create on top. We are mapping only age field of lp list with la list.
la=lp.stream().map(p->p.getAge()).collect(Collectors.toList());
// To map list on another list ls which we create on top. We are mapping only salary field of lp list with ls list.
ls=lp.stream().map(p->p.getSalary()).collect(Collectors.toList());
// To map list on another list ln which we create on top. We are mapping only name field of lp list with ln list.
ln=lp.stream().map(p->p.getName()).collect(Collectors.toList());
// To get maximum integer value in list. If list have multiple integer value then it will only consider first one
System.out.println(la.stream().max(Comparator.comparing(Integer::valueOf)).get());
// To get minimum integer value in list. If list have multiple integer value then it will only consider first one
System.out.println(la.stream().min(Comparator.comparing(Integer::valueOf)).get());
// To get maximum double value in list. If list have multiple double value then it will only consider first one
System.out.println(ls.stream().max(Comparator.comparing(Double::valueOf)).get());
// To get maximum double value in list. If list have multiple double value then it will only consider first one
System.out.println(ls.stream().min(Comparator.comparing(Double::valueOf)).get());
// To get maximum string value in list. If list have multiple string value then it will only consider first one
System.out.println(ln.stream().max(Comparator.comparing(String::valueOf)).get());
// To get maximum string value in list. If list have multiple string value then it will only consider first one
System.out.println(ln.stream().min(Comparator.comparing(String::valueOf)).get());
// Sort list la in ascending order
la.stream().sorted().forEach(System.out::println);
// Sort list ls in ascending order
ls.stream().sorted().forEach(System.out::println);
// Sort list ln in ascending order
ln.stream().sorted().forEach(System.out::println);
// Sort list ln in descending order
la.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println);
// Sort list ls in descending order
ls.stream().sorted(Comparator.reverseOrder()).forEach(System.out::println);
// Sort list ln in descending order
ln.stream().sorted(Comparator.naturalOrder()).forEach(System.out::println);
// Sort list lp in ascending order on the basis of age
lp.stream().sorted(Comparator.comparingInt(p->p.getAge())).forEach(System.out::println);
// Sort list lp in ascending order on the basis of name
lp.stream().sorted(Comparator.comparing(p->p.getName())).forEach(System.out::println);
// Sort list lp in ascending order on the basis of salary and print only name
lp.stream().sorted(Comparator.comparing(p->p.getSalary())).forEach(System.out.println(p.getName);
// Sort list lp in ascending order on the basis of salary
lp.stream().sorted(Comparator.comparingDouble(p->p.getSalary())).forEach(System.out::println);
}
}