class="java">public class Test { public static void main(String[] args) { Map<String ,List<String>> map = new HashMap<String ,List<String>>(); Set<String> nameSet = new HashSet<String>(); List<Contract> list = getContracts(); for(Contract c : list){ nameSet.add(c.getName()); } for(String name : nameSet){ List<String> phones = new ArrayList<String>(); for(Contract c : list){ if(name.equals(c.getName())){ phones.add(c.getPhone()); } } map.put(name, phones); } System.out.println(map); } public static List<Contract> getContracts(){ String[] name = {"Tom","Jack","Mike","Tony"}; List<Contract> list = new ArrayList<Contract>(); list.add(new Contract(101,name[0],"11111111")); list.add(new Contract(102,name[0],"22222222")); list.add(new Contract(103,name[1],"33333333")); list.add(new Contract(104,name[1],"44444444")); list.add(new Contract(105,name[2],"55555555")); list.add(new Contract(106,name[2],"66666666")); list.add(new Contract(107,name[3],"77777777")); list.add(new Contract(108,name[3],"88888888")); return list; } }
?