日期:2014-05-20 浏览次数:20834 次
public static void testMap() {
List<String[]> list = createDatas();
Map<String, List<String>> map = new HashMap<String, List<String>>();
for (String[] arr : list) {
if (2 < arr.length) {
throw new IllegalArgumentException("out of index");
}
String a = arr[0];
String b = arr[1];
if (!map.containsKey(a)) {
List<String> tmpList = new ArrayList<String>();
tmpList.add(b);
map.put(a, tmpList);
} else {
map.get(a).add(b);
}
}
Iterator<Entry<String, List<String>>> it = map.entrySet().iterator();
while (it.hasNext()) {
Entry<String, List<String>> entry = it.next();
System.out.println(entry);
}
// ========= output=========
// 1=[1, 2, 2, 2]
// 0=[1, 2]
}
private static List<String[]> createDatas() {
String[] s1 = { "0", "1" };
String[] s2 = { "0", "2" };
String[] s3 = { "1", "1" };
String[] s4 = { "1", "2" };
String[] s5 = { "1", "2" };
String[] s6 = { "1", "2" };
List<String[]> list = new ArrayList<String[]>();
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
list.add(s6);
return list;
}