Map的迭代方式有哪几种? |
您所在的位置:网站首页 › 迭代方法有哪几种 › Map的迭代方式有哪几种? |
Map的形式分为四种: Map map=new HashMap(); map.put("1","没有什么不可以"); map.put("2","一定可以做到"); map.put("3","一定行"); 1、通过获取map.keySet的方式 for(String key:map.keySet()){ System.out.println("key:"+key+",value:"+map.get(key)); } 2、通过迭代器的方式 Iterator it=map.entrySet().iterator(); while(it.hasNext()){ Map.Entry entry=it.next(); System.out.println("key:"+entry.getKey()+",value:"+entry.getValue()); } 3、使用map.entrySet的方式 for(Map.Entry entry:map.entrySet){ System.out.println("key:"+entry.getKey()+",value:"+entry.getValue()); } 4、遍历map.values的方式 for(String value:map.getValues()){ System.out.println(value); } |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |