Question: Find out the employee whose age = 41
Suppose you are having the below Employee Class:
class Employee{ int age; String name; public Employee(int age, String name) { this.age=age; this.name=name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return String.valueOf(age) + "-" +name; } }
And having a list containing the Employee object. So below would be its solution:
public class Manager { public static void main(String[] args) { List<Employee> myList=new ArrayList(); myList.add(new Employee(41,"Pressy")); myList.add(new Employee(10,"Paddy")); myList.add(new Employee(35,"Ashy")); myList.stream().filter(x -> x.getAge() == 41).forEach(System.out::println); } }