> 文档中心 > Map循环遍历的常用方法

Map循环遍历的常用方法

常用Map循环遍历方法

  Map map = new HashMap(); map.put("1", "a"); map.put("2", "b"); map.put("3", "c"); System.out.println(map.containsKey("4")); //构造测试数据 List<Map> list = new ArrayList(); HashMap map1 = new HashMap() {     {  put("a", 16);     } }; list.add(map1); HashMap map2 = new HashMap() {     {  put("a", 17);     } }; list.add(map2); HashMap map3 = new HashMap() {     {  put("a", 18);     } }; list.add(map3); Iterator<Map> iterator = list.iterator(); while (iterator.hasNext()) {     Map info = iterator.next();     if (Integer.valueOf(info.get("a").toString()) > 16) {  iterator.remove();     } } for (Map mapz : list) {     System.out.println(mapz.get("a")); } Map newmap = new HashMap(); newmap.put("1", "A"); newmap.put("2", "B"); newmap.put("3", "C");//方法一 Set set = newmap.keySet(); Iterator iterator1 = set.iterator(); while (iterator1.hasNext()) {     Object next = iterator1.next();     System.out.println(next + "/" + newmap.get(next)); }//方法二 Set<Map.Entry> setx = newmap.entrySet(); Iterator<Map.Entry> iteratorx = setx.iterator(); while (iteratorx.hasNext()) {     Map.Entry mapx = iteratorx.next();     System.out.println(mapx.getKey() + "/" + mapx.getValue()); } //方法三 Set<Map.Entry> entries = newmap.entrySet(); for (Map.Entry entry : entries) {    System.out.println(entry.getKey() + ",value为:" + entry.getValue()); }