欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

Map集合的四种遍历方式

时间:2023-07-07
Map集合的遍历1-键找值 (Iterator 遍历)

Map map = new HashMap(); map.put("a","java"); map.put("b","c++"); map.put("c","php"); map.put("d","python"); map.put("e","erlang"); Set set = map.keySet(); Iterator it = set.iterator(); while (it.hasNext()){ String key = it.next(); String values = map.get(key); System.out.println(key+"=="+values); }

Map集合的遍历2-键值对映射关系(Iterator 遍历)

Map map = new HashMap(); map.put("a","java"); map.put("b","c++"); map.put("c","php"); map.put("d","python"); map.put("e","erlang"); Set> set = map.entrySet(); Iterator> it = set.iterator(); while (it.hasNext()){ Map.Entry entry = it.next(); String key = entry.getKey(); String value = entry.getValue(); System.out.println(key+"=="+value); }

Map集合的遍历3-键找值(for遍历)

Map map = new HashMap(); map.put("a","java"); map.put("b","c++"); map.put("c","php"); map.put("d","python"); map.put("e","erlang"); for(String key :map.keySet()){ System.out.println(key+"=="+map.get(key)); }

Map集合的遍历4-键值对映射关系(for遍历)

Map map = new HashMap(); map.put("a","java"); map.put("b","c++"); map.put("c","php"); map.put("d","python"); map.put("e","erlang"); for(Map.Entry entry:map.entrySet()){ System.out.println(entry.getKey()+"=="+entry.getValue()); }

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。