一、List嵌套List
public static void main(String[] args) {
List<List<String>> listList = new ArrayList<>();
ArrayList<String> strings1 = new ArrayList<>();
ArrayList<String> strings2 = new ArrayList<>();
Collections.addAll(strings2,"nihao","wohao","dajiahao");
Collections.addAll(strings1,"xiaohua","xiaocao","pugongying");
Collections.addAll(listList,strings2,strings1);
System.out.println(listList);
for (List<String> strings : listList) {
for (String string : strings) {
System.out.println(string);
}
}
}
二、List嵌套Map
private static void fun3() {
HashMap<Integer, String> map1 = new HashMap<>();
HashMap<Integer, String> map2 = new HashMap<>();
map1.put(1,"张三");
map1.put(2,"李四");
map1.put(3,"王五");
map2.put(1,"Hyunjin");
map2.put(2,"Felix");
map2.put(3,"Leeknow");
List<HashMap<Integer,String>> list=new ArrayList<>();
list.add(map1);
list.add(map2);
System.out.println(list);
for (HashMap<Integer, String> map : list) {
Set<Map.Entry<Integer, String>> entries = map.entrySet();
for (Map.Entry<Integer, String> entry : entries) {
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"="+value);
}
}
}
三、Map嵌套Map
private static void fun4() {
HashMap<Integer, String> map1 = new HashMap<>();
HashMap<Integer, String> map2 = new HashMap<>();
map1.put(1,"张三");
map1.put(2,"李四");
map1.put(3,"王五");
map2.put(1,"Hyunjin");
map2.put(2,"Felix");
map2.put(3,"Leeknow");
HashMap<String, HashMap<Integer, String>> stringHashMapHashMap = new HashMap<>();
stringHashMapHashMap.put("javase",map1);
stringHashMapHashMap.put("javaee",map2);
Set<Map.Entry<String, HashMap<Integer, String>>> entries = stringHashMapHashMap.entrySet();
for (Map.Entry<String, HashMap<Integer, String>> entry : entries) {
String key = entry.getKey();
HashMap<Integer, String> value = entry.getValue();
Set<Map.Entry<Integer, String>> entries1 = value.entrySet();
for (Map.Entry<Integer, String> integerStringEntry : entries1) {
Integer key1 = integerStringEntry.getKey();
String value1 = integerStringEntry.getValue();
System.out.println(key+"..."+key1+"..."+value1);
}
}
}