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());
CodePudding user response:
Well this is a multi-step process. Firstly, I don't see any point in creating an intermediate stream, just for the sake of creating a Collection
. There are more Collection
library methods such as good old Arrays.asList
which you would use to create a collection easily without creating unnecessary intermediate objects. Here's a linear time solution to your problem.
final List<String> filterList = Arrays.asList("pleasanton", "Atlanta", "Cumming", "Boston");
final Map<String, Integer> posMap = IntStream.range(0, filterList.size()).boxed()
.collect(Collectors.toMap(filterList::get, i -> i));
final Set<String> cities = new HashSet<>(filterList);
final Collection<Employee> sortedEmps = list1.stream().filter(e -> cities.contains(e.getCity()))
.sorted(Comparator.comparing(e -> posMap.get(e.getCity()))).collect(Collectors.toList());
Note that you sort the elements based on their position in the input array, and the use of a Set
to check the containment brings otherwise quadratic time solution to a linear one.
CodePudding user response:
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();
});
}
}