日期:2014-05-20 浏览次数:20869 次
public class Person {
private String name;
private String doSth;
}
这样应该差不多吧
List<Person> list = new ArrayList<Person>();
Person p1 = new Person();
p1.setName("P1");
Person p2 = new Person();
p2.setName("P2");
Person p3 = new Person();
p3.setName("P3");
Person p4 = new Person();
p4.setName("P1");
Person p5 = new Person();
p5.setName("P1");
Person p6 = new Person();
p6.setName("P2");
Person p7 = new Person();
p7.setName("P3");
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
list.add(p5);
list.add(p6);
list.add(p7);
Map<String,List<Person>> map = new HashMap<String,List<Person>>();
for(Person person : list){
if(map.containsKey(person.getName())){
List<Person> li = map.get(person.getName());
li.add(person);
map.put(person.getName(), li);
}else{
List<Person> li = new ArrayList<Person>();
li.add(person);
map.put(person.getName(), li);
}
}