ArrayList嵌套HashMap
public class Include {
public static void main(String[] args) {
HashMap<String, String> hm1 = new HashMap<String, String>();
hm1.put("周瑜", "小乔");
hm1.put("吕布", "貂蝉");
HashMap<String, String> hm2 = new HashMap<String, String>();
hm2.put("郭靖", "黄蓉");
hm2.put("杨过", "小龙女");
ArrayList<HashMap<String, String>> array = new ArrayList<HashMap<String, String>>();
array.add(hm1);
array.add(hm2);
// 遍历
for (HashMap<String, String> hm : array) {
Set<String> set = hm.keySet();
for (String key : set) {
String value = hm.get(key);
System.out.println(key + "---" + value);
}
}
}
}
输出:
吕布---貂蝉
周瑜---小乔
杨过---小龙女
郭靖---黄蓉
ArrayList 嵌套 HashMap 一般用于将多个键值对集合进行汇总。
HashMap嵌套ArrayList
public class Include {
public static void main(String[] args) {// 创建集合对象
ArrayList<String> list1 = new ArrayList<String>();
list1.add("关羽");
list1.add("吕布");
ArrayList<String> list2 = new ArrayList<String>();
list2.add("孙悟空");
list2.add("猪八戒");
HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>();
hm.put("三国演义", list1);
hm.put("西游记", list2);
// 遍历
Set<String> set = hm.keySet();
for(String key : set){
System.out.println(key);
ArrayList<String> array = hm.get(key);
for(String value : array){
System.out.println("\t" + value);
}
}
}
}
输出:
三国演义
关羽
吕布
西游记
孙悟空
猪八戒
HashMap 嵌套 ArrayList 一般用于将单列集合进行分类,并设置分类的属性。
HashMap嵌套HashMap
public class Include {
public static void main(String[] args) {
HashMap<String, String> xyMap = new HashMap<String, String>();
xyMap.put("孙悟空", "金箍棒");
xyMap.put("猪八戒", "九齿钉耙");
HashMap<String, String> sgMap = new HashMap<String, String>();
sgMap.put("吕布", "方天画戟");
sgMap.put("关羽", "青龙刀");
// 把子集合添加到四大名著集合
HashMap<String, HashMap<String, String>> sdmzMap = new HashMap<String, HashMap<String, String>>();
sdmzMap.put("西游记", xyMap);
sdmzMap.put("三国演义", sgMap);
//遍历集合
Set<String> sdmzMapSet = sdmzMap.keySet();
for(String sdmzMapKey : sdmzMapSet){
System.out.println(sdmzMapKey);
HashMap<String, String> subHashMap = sdmzMap.get(sdmzMapKey);
Set<String> keySet = subHashMap.keySet();
for(String key : keySet){
String value = subHashMap.get(key);
System.out.println("\t"+key+":"+value);
}
}
}
}
输出:
三国演义
关羽:青龙刀
吕布:方天画戟
西游记
孙悟空:金箍棒
猪八戒:九齿钉耙
HashMap 嵌套 HashMap 一般用于将多个键值对集合进行分类,并设置分类的属性。
多层嵌套-HashMap嵌套HashMap嵌套ArrayList
public class HashMapDemo {
public static void main(String[] args) {
// 北京地区
ArrayList<Student> czArray = new ArrayList<Student>();
czArray.add(new Student("小明", 11));
czArray.add(new Student("小花", 13));
ArrayList<Student> gzArray = new ArrayList<Student>();
gzArray.add(new Student("赵磊", 17));
gzArray.add(new Student("武鑫", 16));
HashMap<String, ArrayList<Student>> bjXxMap = new HashMap<String, ArrayList<Student>>();
bjXxMap.put("初中", czArray);
bjXxMap.put("高中", gzArray);
// 上海地区
ArrayList<Student> czArray2 = new ArrayList<Student>();
czArray2.add(new Student("小刚", 12));
czArray2.add(new Student("小强", 14));
ArrayList<Student> gzArray2 = new ArrayList<Student>();
gzArray2.add(new Student("安琪", 15));
gzArray2.add(new Student("博宇", 16));
HashMap<String, ArrayList<Student>> shXxMap = new HashMap<String, ArrayList<Student>>();
shXxMap.put("初中", czArray2);
shXxMap.put("高中", gzArray2);
// 创建大集合
HashMap<String, HashMap<String, ArrayList<Student>>> xxMap = new HashMap<String, HashMap<String, ArrayList<Student>>>();
xxMap.put("北京地区", bjXxMap);
xxMap.put("上海地区", shXxMap);
// 遍历集合
Set<String> xxMapSet = xxMap.keySet();
for (String xxMapKey : xxMapSet) {
System.out.println(xxMapKey);
HashMap<String, ArrayList<Student>> xxMapValue = xxMap
.get(xxMapKey);
Set<String> xxMapValueSet = xxMapValue.keySet();
for (String xxMapValueKey : xxMapValueSet) {
System.out.println("\t" + xxMapValueKey);
ArrayList<Student> xxMapValueValue = xxMapValue
.get(xxMapValueKey);
for (Student s : xxMapValueValue) {
System.out.println("\t\t" + s.getName() + "---"
+ s.getAge());
}
}
}
}
}
输出:
北京地区
高中
赵磊---17
武鑫---16
初中
小明---11
小花---13
上海地区
高中
安琪---15
博宇---16
初中
小刚---12
小强---14
多层嵌套一般用于需要多层分类归纳的情况。