一、hashmap中核心几个要点
1、hashmap允许null值null键;
2、hashmap实现了map接口,而hashtable继承自Dictionary类
3、hashmap中put(K,V)--如果key相同,则覆盖原来的oldValue;get(Object key)--根据键key获取对应的value值
4、hashmap里面的元素是无序的,如果在实际项目中要保持元素有序,应该使用LinkedHashMap。
二、对treemap的理解
1、TreeMap键不能为null,如果调用treemap对象put(K,V)中,k=null,则会出现如下错误:
// TreeMap的键值不能是null Map<String, String> strMap2 = new TreeMap<String, String>(); strMap2.put(null, "1"); // strMap2.put("", null); // strMap2.put(" ", "3"); // strMap2.put(null, "4"); System.out.println(strMap2.get(null)); // for (String s : strMap2.keySet()) { // System.out.println(s); // } // for (String s : strMap2.values()) { // System.out.println(s); // }
运行结果为:==========================treemap数据结构========================= Exception in thread "main" java.lang.NullPointerException at java.util.TreeMap.compare(TreeMap.java:1294) at java.util.TreeMap.put(TreeMap.java:538) at thinkinjava.MapTest.main(MapTest.java:85)