如何根据“城市筛选列表”筛选员工列表,并使用java 8维护筛选顺序。
我希望根据另一个列表( filterList : referring )筛选Employee (FilterList)对象,并创建新列表(List2),并使用Java8维护hte filterList城市订单。
重要注意事项:这里的新列表应该与filterList(城市名称)的顺序相同。
投入:
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 = 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());发布于 2022-07-21 05:26:22
试试这个:
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();
});
}
}发布于 2022-08-14 00:14:56
与使用indexOf进行比较不同,我们可以使用Comparator.comparingInt:
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();
});
}}
https://stackoverflow.com/questions/73060934
复制相似问题