java Map集合嵌套,value为Map和value为List

孤人 提交于 2019-12-01 16:26:45

java Map集合嵌套,value为Map和value为List

2013年12月13日 23:13:34 wuer0520 阅读数:12476


 
  1. import java.util.ArrayList;

  2. import java.util.HashMap;

  3. import java.util.Iterator;

  4. import java.util.List;

  5. import java.util.Map;

  6. import java.util.Set;

  7.  
  8.  
  9. public class MapDemo {

  10.  
  11. public static void main(String[] args) {

  12. System.out.println("Map集合的值为Map");

  13. oneToMap();

  14.  
  15. System.out.println("Map集合的值为List,特别常用!必须会!");

  16. oneToList();

  17. }

  18.  
  19. /*

  20. * 这种是把班级和学生作为映射

  21. * 而且又把学号和姓名做了映射

  22. */

  23. public static void oneToMap() {

  24. Map<String,Map<String,String>> jiSuanJi = new HashMap<String,Map<String,String>>();

  25. Map<String,String> ruanJian = new HashMap<String,String>();

  26. Map<String,String> wangZhan = new HashMap<String,String>();

  27.  
  28. /*

  29. * 千万不要每次都new一个进去,这样就不是原来的集合了

  30. * 结果yingyong这个key对应的value集合是null

  31. * 遍历Map的时候还会出现空指针错误

  32. */

  33. //jiSuanJi.put("yingyong", (Map<String, String>) new HashMap().put("01", "haha"));

  34. //jiSuanJi.put("yingyong", (Map<String, String>) new HashMap().put("02", "xixi"));

  35.  
  36. /*

  37. * 要使用下面这种方式,先把集合定义好,

  38. * 把映射关系设置好,再去给集合添加元素

  39. */

  40. jiSuanJi.put("ruanJian", ruanJian);

  41. jiSuanJi.put("wangZhan", wangZhan);

  42.  
  43. ruanJian.put("01", "zhangsan");

  44. ruanJian.put("02", "lisi");

  45.  
  46. wangZhan.put("01", "zhaoliu");

  47. wangZhan.put("02", "zhouqi");

  48.  
  49. Set<String> keySet = jiSuanJi.keySet();

  50. for(Iterator<String> it = keySet.iterator();it.hasNext();) {

  51. String key = it.next();

  52. System.out.println(key);

  53. Map<String,String> map = jiSuanJi.get(key);

  54. Set<Map.Entry<String, String>> entrySet = map.entrySet();

  55. for(Iterator<Map.Entry<String, String>> it2 = entrySet.iterator();it2.hasNext();) {

  56. Map.Entry<String, String> me = it2.next();

  57. System.out.println(me.getKey() + ".." + me.getValue());

  58. }

  59. }

  60. }

  61.  
  62. /*

  63. * 这种把班级和学生做了映射

  64. * 学生类中封装了学号和姓名

  65. */

  66. public static void oneToList() {

  67. Map<String,List<PersonDemo>> jiSuanJi = new HashMap<String,List<PersonDemo>>();

  68. List<PersonDemo> ruanJian = new ArrayList<PersonDemo>();

  69. List<PersonDemo> wangZhan = new ArrayList<PersonDemo>();

  70.  
  71. jiSuanJi.put("ruanJian", ruanJian);

  72. jiSuanJi.put("wangZhan", wangZhan);

  73.  
  74. ruanJian.add(new PersonDemo("01","zhangsan"));

  75. ruanJian.add(new PersonDemo("02","lisi"));

  76. wangZhan.add(new PersonDemo("01","wangwu"));

  77. wangZhan.add(new PersonDemo("02","zhaoliu"));

  78.  
  79. Set<String> keySet = jiSuanJi.keySet();

  80. for(Iterator<String> it = keySet.iterator();it.hasNext();){

  81. String key = it.next();

  82. System.out.println(key);

  83. List<PersonDemo> list = jiSuanJi.get(key);

  84. for(Iterator<PersonDemo> it2 = list.iterator();it2.hasNext();) {

  85. PersonDemo pd = it2.next();

  86. System.out.println(pd);

  87. }

  88. }

  89. }

  90.  
  91. }

  92.  
  93. class PersonDemo {

  94.  
  95. private String id;

  96. private String name;

  97.  
  98. public PersonDemo(String id, String name) {

  99. super();

  100. this.id = id;

  101. this.name = name;

  102. }

  103. public String getId() {

  104. return id;

  105. }

  106. public void setId(String id) {

  107. this.id = id;

  108. }

  109. public String getName() {

  110. return name;

  111. }

  112. public void setName(String name) {

  113. this.name = name;

  114. }

  115. @Override

  116. public String toString() {

  117. return this.id + "..." + this.name;

  118. }

  119.  
  120.  
  121. }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!