There's probably a library that can do this more simply, but it's not too hard to do it manually:
List<Person> allPeople; // your list of all people
Map<String, List<Person>> map = new HashMap<String, List<Person>>();
for (Person person : allPeople) {
String key = person.getName();
if (map.get(key) == null) {
map.put(key, new ArrayList<Person>());
}
map.get(key).add(person);
}
List<Person> davids = map.get("David");
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…