Search code examples
javacollectionsjava-8

How can I filter employee list based on the "City filter list" and maintain the filter order using java 8


How can i filter employee list based on the "City filter list" and maintain the filter order using java 8.

I want to filter Employee (list1) objects by city name based on the another list(filterList : referring City) and create new list(list2) and maintain hte filterList City order using Java 8.

Important Note : Here new list should be in same order where the filterList(City name) there.

Input :

List<Employee> list1 = Stream.of(
                        new Employee("100","Boston","Massachusetts"),
                        new Employee("400","Atlanta","Georgia"),
                        new Employee("300","pleasanton","California"),
                        new Employee("200","Decatur","Texas"),
                        new Employee("500","Cumming","Atlanta"),
                        new Employee("98","sula","Maine"),
                        new Employee("156","Duluth","Ohio"))
                .collect(Collectors.toList());

Filter List :

List<String> filterList = Stream.of("pleasanton", "Atlanta", "Cumming", "Boston").collect(Collectors.toList());

Expected output :

List<Employee> list2 = Stream.of(
                        new Employee("300","pleasanton","California"),
                        new Employee("400","Atlanta","Georgia"),
                        new Employee("500","Cumming","Atlanta"),
                        new Employee("100","Boston","Massachusetts"))
                .collect(Collectors.toList());

Solution

  • Try this:

    import java.util.*;
    import java.util.stream.*;
    class Employee {
        private String salary;
        private String city;
        private String state;
        
        public Employee(String salary, String city, String state) {
            this.salary = salary;
            this.city = city;
            this.state = state;
        }
        
        public String getSalary() {
            return salary;
        }
        
        public String getCity() {
            return city;
        }
        
        public String getState() {
            return state;
        }
    }
    
    public class Main {
        public static void main(String[] args) {
            List<Employee> list1 = Stream.of(
                            new Employee("100","Boston","Massachusetts"),
                            new Employee("400","Atlanta","Georgia"),
                            new Employee("300","pleasanton","California"),
                            new Employee("200","Decatur","Texas"),
                            new Employee("500","Cumming","Atlanta"),
                            new Employee("98","sula","Maine"),
                            new Employee("156","Duluth","Ohio"))
                    .collect(Collectors.toList());
            
            List<String> filterList = Stream.of("pleasanton", "Atlanta", "Cumming", "Boston").collect(Collectors.toList());
            
            List<Employee> list2 = list1.stream().filter(e -> filterList.contains(e.getCity())).sorted((a, b) -> filterList.indexOf(a.getCity()) - filterList.indexOf(b.getCity())).collect(Collectors.toList());
        list2.stream().forEach(e -> {
                    System.out.println("City " + e.getCity());
                    System.out.println("State " + e.getState());
                    System.out.println("Salary " + e.getSalary());
                    System.out.println();
            });
        }
    }