当前位置:   article > 正文

HashMap嵌套HashMap集合

hashmap嵌套hashmap
  1. package com.cnd05;
  2. import java.util.HashMap;
  3. import java.util.Set;
  4. //HashMap嵌套HashMap结构
  5. public class TreeMapDemo2 {
  6. public static void main(String[] args) {
  7. //创建集合
  8. HashMap<String,HashMap<String,Integer>> sumMap=new HashMap<String,HashMap<String,Integer>>();
  9. //先存储元素,后遍历元素
  10. HashMap<String,Integer> hm1=new HashMap<String,Integer>();
  11. hm1.put("高跃", 22);
  12. hm1.put("李杰", 20);
  13. HashMap<String,Integer> hm2=new HashMap<String,Integer>();
  14. hm2.put("方乐", 24);
  15. hm2.put("贾芳", 23);
  16. //存储元素
  17. sumMap.put("一年级", hm1);
  18. sumMap.put("二年级", hm2);
  19. //遍历元素
  20. Set<String> sumMapSet=sumMap.keySet();
  21. for(String key:sumMapSet) {
  22. System.out.println(key);
  23. HashMap<String,Integer> subMapValue=sumMap.get(key);
  24. //遍历键值(HashMap结构)
  25. Set<String> subMapValueSet = subMapValue.keySet();
  26. for(String key2:subMapValueSet) {
  27. Integer subMapValueSetValue=subMapValue.get(key2);
  28. System.out.println("\t"+key2+"---"+subMapValueSetValue);
  29. }
  30. }
  31. }
  32. }

运行结果为:

一年级
    高跃---22
    李杰---20
二年级
    贾芳---23
    方乐---24
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/489029
推荐阅读
相关标签
  

闽ICP备14008679号