Collectors.groupingBy
+Collectors.maxBy
应该可以构建按名称分组的人员地图,然后选择最大值:
List<Person> persons = Arrays.asList(
new Person("Jerry", 123),
new Person("Tom", 234),
new Person("Jerry", 456),
new Person("Jake", 789)
);
List<Person> maxById = persons
.stream()
.collect(Collectors.groupingBy(
Person::getName,
Collectors.maxBy(Comparator.comparingInt(Person::getID))
))
.values() // Collection<Optional<Person>>
.stream() // Stream<Optional<Person>>
.map(opt -> opt.orElse(null))
.collect(Collectors.toList());
System.out.println(maxById);
输出:
[789: Jake, 234: Tom, 456: Jerry]
更新
有没有办法获得一个单独的 Person 对象列表,因为它们在这个 stream() 中是重复的而被删除?
最好将分组项目收集在一个列表中,然后在一些包装类中进行转换,以提供有关maxById
人员和重复数据删除人员列表的信息:
class PersonList {
private final Person max;
private final List<Person> deduped;
public PersonList(List<Person> group) {
this.max = Collections.max(group, Comparator.comparingInt(Person::getID));
this.deduped = new ArrayList<>(group);
this.deduped.removeIf(p -> p.getID() == max.getID());
}
@Override
public String toString() {
return "{max: " + max + "; deduped: " + deduped + "}";
}
}
然后应该像这样收集人员:
List<PersonList> maxByIdDetails = new ArrayList<>(persons
.stream()
.collect(Collectors.groupingBy(
Person::getName,
LinkedHashMap::new,
Collectors.collectingAndThen(
Collectors.toList(), PersonList::new
)
))
.values()); // Collection<PersonList>
maxByIdDetails.forEach(System.out::println);
输出:
{max: 456: Jerry; deduped: [123: Jerry]}
{max: 234: Tom; deduped: []}
{max: 789: Jake; deduped: []}
更新 2
获取重复人员列表:
List<Person> duplicates = persons
.stream()
.collect(Collectors.groupingBy(Person::getName))
.values() // Collection<List<Person>>
.stream() // Stream<List<Person>>
.map(MyClass::removeMax)
.flatMap(List::stream) // Stream<Person>
.collect(Collectors.toList()); // List<Person>
System.out.println(duplicates);
输出:
[123: Jerry]
whereremoveMax
可以这样实现:
private static List<Person> removeMax(List<Person> group) {
List<Person> dupes = new ArrayList<>();
Person max = null;
for (Person p : group) {
Person duped = null;
if (null == max) {
max = p;
} else if (p.getID() > max.getID()) {
duped = max;
max = p;
} else {
duped = p;
}
if (null != duped) {
dupes.add(duped);
}
}
return dupes;
}
或者,如果hashCode
和equals
在 class 中正确实现Person
,则可以使用以下方法计算两个列表之间的差异removeAll
:
List<Person> duplicates2 = new ArrayList<>(persons);
duplicates2.removeAll(maxById);
System.out.println(duplicates2);