直接上代码
public static void main(String[] args){ Map<Object, Object> hashMap = new HashMap<Object, Object>(){{ put("1",1); put("2",1); put("3",1); put("4",1); put("5",1); put("6",1); put("7",1); put("8",1); put("9",1); }}; //lambda hashMap.forEach((k,v) -> System.out.println("key:"+k+","+"value:"+v)); String a = ""; //常用的 for(Object key:hashMap.keySet()){ System.out.println(key+":"+hashMap.get(key)); } //大容量的时候 for(Map.Entry<Object,Object> entry:hashMap.entrySet()){ System.out.println("key:"+entry.getKey()); System.out.println("value:"+entry.getValue()); } //迭代器 Iterator li = hashMap.entrySet().iterator(); while (li.hasNext()){ Map.Entry<Object,Object> ll = (Map.Entry<Object, Object>) li.next(); ll.getKey(); ll.getValue(); } }