package 常用的遍历方法.forTest; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; public class CircleTest { public static void main(String[] args) { Map<String,Object> map = new HashMap<String,Object>(); map.put("accident", "意外险"); map.put("properties", "财产险"); map.put("biz", "商业险"); map.put("force", "交强险"); //1、迭代器 Iterator<Map.Entry<String, Object>> iter = map.entrySet().iterator(); //判断往下还有没有数据 while(iter.hasNext()){ //有的话取出下面的数据 Entry<String, Object> entry = iter.next(); String key = entry.getKey(); String value = (String)entry.getValue(); if("biz".equals(entry.getKey())){ System.out.println("这个是商业险"); } System.out.println(key + " :" + value); } //2、foreach循环 //获取key + value for (Object key : map.keySet()) { String value = (String)map.get(key); System.out.println(key + " : " + value); } //获取value for (Object value : map.values()) { System.out.println(value); } //3、当容量特别大的时候 for (Entry<String, Object> entry : map.entrySet()) { /*iter : biz=商业险 force=交强险 accident=意外险 properties=财产险*/ System.out.println(entry.getKey() + " : " + entry.getValue()); } } }
最新评论