(一)HashMap取值--->不是按照插入顺序
HashMap<String,Integer> hm=new HashMap<String, Integer>(); hm.put("大学语文",3); hm.put("英语",1); hm.put("音乐鉴赏",5); hm.put("数学",2); hm.put("形式政策",4); for (Map.Entry<String ,Integer> entry:hm.entrySet()){ System.out.println(entry.getKey()+" : "+entry.getValue()); }
结果:
(二):LinkedHashMap的取值--->是按照插入取值
LinkedHashMap<String,Integer> lhm=new LinkedHashMap<String,Integer>(); lhm.put("化学",1); lhm.put("生物",2); lhm.put("物理",3); lhm.put("语文",4); lhm.entrySet(); for (Map.Entry<String,Integer> entry:lhm.entrySet()){ System.out.println(entry.getKey()+" : "+entry.getValue()); }
结果: