Hashmap,Hashtable,TreeMap,LinkedHashMap之间的区别
2018-06-03 09:53
344 查看
1,Hashmap是我们用得比较多的集合,产生于jdk 1.2版本,源码put方法有个判断:
if(key==null)
return putForNullKey(value);
可以看出hashmap是支持存放键key值为null;
HashMap是线程不安全的,所以也相对更快
2,Hashtable产生于jdk 1.1版本,相对于HashMap 的方法,HashTable是线程安全的,在put方法上有个判断:
if (value == null) {
throw new NullPointerException();
}
和 int hash = hash(key); //获取hash值
说明HashTable是 不支持key,value为null的 会报空指针异常
3,TreeMap是有顺序的
TreeMap实现了SortMap接口 说明根据 存入集合,是有顺序的,默认是按键值的升序排的序
可用iterator遍历,得到想要的序
treeMap的键值key不能为null
4,LinkedHashMap 插入是有顺序的, 用iterator遍历LinkedHashMap ,先进先出,基于链表的设计, 也可以在构造时带参,根据参数采取排序 LinkedHashMap 是支持存放键key值为null;
相关文章推荐
- HashMap、Hashtable、LinkedHashMap 和TreeMap之间的区别
- HashTable,HashMap,LinkedHashMap,TreeMap之间的区别。
- HashMap、Hashtable、LinkedHashMap、TreeMap、ConcurrentHashMap之间的区别-yellowcong
- HashMap、TreeMap、LinkedHashMap、hashtable的区别
- HashMap、HashTable、LinkedHashMap和TreeMap用法和区别
- Java中HashMap,TreeMap,LinkedHashMap,HashTable的区别
- ArrayList 、LinkList、List 区别 & 迭代器iterator的使用 & HashMap、Hashtable、LinkedHashMap、TreeMap
- HashMap、TreeMap、Hashtable、LinkedHashMap区别
- HashMap、HashTable、LinkedHashMap和TreeMap用法和区别
- HashMap, TreeMap, LinkedHashMap, Hashtable区别(二)
- java中HashMap,LinkedHashMap,TreeMap,HashTable的区别
- HashMap、HashTable、LinkedHashMap和TreeMap用法和区别
- java中HashMap,LinkedHashMap,TreeMap,HashTable的区别
- HashMap、HashTable、LinkedHashMap和TreeMap的区别
- HashMap、HashTable、LinkedHashMap和TreeMap用法和区别
- HashMap, TreeMap, LinkedHashMap, Hashtable区别(一)
- HashTable、HashMapTreeMap、LinkedHashMap的区别
- java中HashMap,LinkedHashMap,TreeMap,HashTable的区别
- java中HashMap,LinkedHashMap,TreeMap,HashTable的区别
- HashMap,LinkedHashMap,TreeMap,HashTable的区别