Java examples for Lambda Stream:Filter
Display Employees with salaries in the range $40000-$60000 sorted into ascending order by salary
import java.util.Arrays; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.TreeMap; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; public class Main { public static void main(String[] args) {//from w w w . j a va 2 s . c o m Employee[] employees = { new Employee("J", "Red", 51000, "IT"), new Employee("A", "Green", 71600, "IT"), new Employee("M", "Black", 35187.5, "Sales"), new Employee("K", "Yellow", 47100.77, "Marketing"), new Employee("L", "Pink", 62001, "IT"), new Employee("D", "Blue", 32001, "Sales"), new Employee("W", "Brown", 42361.4, "Marketing")}; // get List view of the Employees List<Employee> list = Arrays.asList(employees); // display all Employees System.out.println("Complete Employee list:"); list.stream().forEach(System.out::println); // Predicate that returns true for salaries in the range $40000-$60000 Predicate<Employee> fourToSixThousand = e -> (e.getSalary() >= 40000 && e.getSalary() <= 60000); // Display Employees with salaries in the range $4000-$6000 // sorted into ascending order by salary System.out.printf( "%nEmployees earning $40000-$60000 per month sorted by salary:%n"); list.stream() .filter(fourToSixThousand) .sorted(Comparator.comparing(Employee::getSalary)) .forEach(System.out::println); } } class Employee { private String firstName; private String lastName; private double salary; private String department; // constructor public Employee(String firstName, String lastName, double salary, String department) { this.firstName = firstName; this.lastName = lastName; this.salary = salary; this.department = department; } // set firstName public void setFirstName(String firstName) { this.firstName = firstName; } // get firstName public String getFirstName() { return firstName; } // set lastName public void setLastName(String lastName) { this.lastName = lastName; } // get lastName public String getLastName() { return lastName; } // set salary public void setSalary(double salary) { this.salary = salary; } // get salary public double getSalary() { return salary; } // set department public void setDepartment(String department) { this.department = department; } // get department public String getDepartment() { return department; } // return Employee's first and last name combined public String getName() { return String.format("%s %s", getFirstName(), getLastName()); } // return a String containing the Employee's information @Override public String toString() { return String.format("%-8s %-8s %8.2f %s", getFirstName(), getLastName(), getSalary(), getDepartment()); } }