[英]How can I filter employee list based on the "City filter list" and maintain the filter order using java 8
如何根据“城市过滤器列表”过滤员工列表并使用 java 8 维护过滤器顺序。
我想根据另一个列表(filterList:引用城市)按城市名称过滤 Employee(list1)对象并创建新列表(list2)并使用 Java 8 维护 hte filterList City 顺序。
重要提示:这里的新列表应该与那里的 filterList(City name) 的顺序相同。
输入:
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());
预期 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());
尝试这个:
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();
});
}
}
嗯,这是一个多步骤的过程。 首先,我认为创建中间流没有任何意义,只是为了创建Collection
。 还有更多的Collection
库方法,例如旧的Arrays.asList
,您可以使用它们轻松创建集合,而无需创建不必要的中间对象。 这是您问题的线性时间解决方案。
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());
请注意,您根据元素在输入数组中的位置对元素进行排序,并且使用Set
来检查包含性会将其他二次时间解带入线性解。
我们可以使用 Comparator.comparingInt,而不是使用 indexOf 进行比较:
class Main {
public static void main(String[] args) {
List<Employee> list = 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> collect = list.stream().filter(re -> filterList.contains(re.getCity())).sorted(Comparator.comparingInt(a -> filterList.indexOf(a.getCity()))).collect(Collectors.toList());
collect.stream()
.forEach(e->{
System.out.println("City" + e.getCity());
System.out.println("State " + e.getState());
System.out.println("Salary " + e.getSalary());
System.out.println();
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.