赞
踩
-
- /**
- * map集合的遍历方式
- */
- public static void main(String[] args) {
- HashMap<String, String> map = new HashMap<>();
- map.put("1","q");
- map.put("8","q2");
- map.put("2","q");
- map.put("3","q");
- map.put("8","q1");
- map.put("4","q");
- map.put("5","q");
- map.put("8","q3");
-
- //遍历一:获取map集合中的每一对键值对,分别获取key和value
- Set<Map.Entry<String, String>> entries = map.entrySet();
- for (Map.Entry<String, String> entry : entries) {
- System.out.println("key:" + entry.getKey() + " , value:" + entry.getValue());
- }
- System.out.println("====================================");
- //遍历二:获取所有的key,遍历key,获取出value
- Set<String> keySet = map.keySet();
- for (String key : keySet) {
- System.out.println("key:" + key + " , value:" + map.get(key));
- }
- System.out.println("====================================");
- //遍历三;迭代器的方式
- Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
- while(iterator.hasNext()){
- Map.Entry<String, String> next = iterator.next();
- System.out.println("key:" + next.getKey() + " , value:" + next.getValue());
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。