Employee object shorting and filtering

 



This  example  explain   how  java 8  filters  the  employee object different point  of 

consideration. 


     1.  First  of all we need to create  a  Employee Class  . 


     2.  Create  a filter class which is going  to filter  the Employee object.


Employee  Class .


import  java.util.*;


public class Employee {


private String name;

private int age;

    private List<String> listOfCities;

     

    public Employee(String name, int age,List<String> listOfCities) {

        super();

        this.name = name;

        this.age = age;

        this.listOfCities=listOfCities;

    }

    public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

public List<String> getListOfCities() {

return listOfCities;

}

public void setListOfCities(List<String> listOfCities) {

this.listOfCities = listOfCities;

}

@Override

    public String toString() {

        return "Employee [name=" + name + ", age=" + age + "]";

    }

}


     

 2.  Employee   main  stream  class  

        

import  java.util.*;

import java.util.stream.Collectors;



public class JavaExamplesStream {


public static void main(String[] args) {

// TODO Auto-generated method stub


List<Employee> listOfEmployees = new ArrayList<>();

 

        Employee e1 = new Employee("Sonason", 24,Arrays.asList("Newyork","Banglore"));

        Employee e2 = new Employee("JohniBhai", 27,Arrays.asList("Paris","Babaland"));

        Employee e3 = new Employee("Rishikesh", 32,Arrays.asList("Pune","India"));

        Employee e4 = new Employee("Himalaya", 22,Arrays.asList("Chennai"," Uttrakhand"));

 

        listOfEmployees.add(e1);

        listOfEmployees.add(e2);

        listOfEmployees.add(e3);

        listOfEmployees.add(e4);

 

        

            

        

    System.out.println("****Sort Employee by the Age********");

    

        

  List<Employee> employees = listOfEmployees.stream()

                .sorted(Comparator.comparing(Employee::getAge))

                .collect(Collectors.toList());

       System.out.println(employees);




System.out.println("****Sort Employee by the Name********");



List<String> listOfEmploye = listOfEmployees.stream()

.map(Employee::getName)

.collect(Collectors.toList());

System.out.println(listOfEmploye);

System.out.println("****Filter the employee by name and age********");


long empCountStartJ = listOfEmployees.stream()

.map(Employee::getName)

.filter(s -> s.startsWith("J"))

.count();

System.out.println(empCountStartJ);

boolean allMatch = listOfEmployees.stream()

.allMatch(e ->e.getAge()>18);


System.out.println("Are all the employess adult: " +allMatch);



}


}


      

      

      




 Example   Output ::::

-------------------------------


****Sort Employee by the Age********

[Employee [name=Amit, age=22], Employee [name=Mohan, age=24], Employee [name=John, age=27], Employee [name=Vaibhav, age=32]]

****Sort Employee by the Name********

[Mohan, John, Vaibhav, Amit]

****Filter the employee by name and age********

1

Are all the employess adult: true





 



       



        

       

Employee object shorting and filtering  Employee  object  shorting    and  filtering Reviewed by Mukesh Jha on 8:49 PM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.