你不需要Guava这样做:
List<Person> people = ...
Map<Name, Person> peopleByName = new HashMap<>();
for (Person person : people) {
// Name is a simple value class with equality based on its fields
Name name = new Name(person.getFirstName(), person.getLastName());
Person firstPersonWithName = peopleByName.get(name);
if (firstPersonWithName == null) {
peopleByName.put(name, person);
} else {
// or whatever you do to mark a duplicate
person.setDuplicateOf(firstPersonWithName);
}
}
Run Code Online (Sandbox Code Playgroud)
也就是说,您可以使用Guava Table而不是a Map,并且避免需要创建Name...使用第一个名称作为行键,使用姓氏作为列键.
另一种选择是用于Multimaps.index按名称索引列表中的所有人.然后,对于映射到特定名称的每个人员列表,第一个人将是列表中具有该名称的第一个人,其他人将是重复的.