我有一个List<Person>
对象。我想从中获取所有id的列表,并且如果可用,我希望id“abc”和“bob”始终出现在列表的第0和第1个索引位置。是否可以使用Java流来实现这一点?
class Person {
private String id;
}
List<Person> allPeople = ...
List<String> allIds = allPeople.stream().map(Person::id).collect(Collectors.toList());
我的方法是:
Set<String> allIds = allPeople.stream().map(Person::id).collect(Collectors.Set());
List<String> orderedIds = new ArrayList<>();
if(allIds.contains("abc")) {
orderedIds.add("abc");
}
if(allIds.contains("bob")) {
orderedIds.add("bob");
}
//Iterate through the set and all add all entries which are not bob and abc in the list.
PriorityQueue
。谢谢。 - Eugene