HashMap、TreeMap、Hashtable、LinkedHashMap区别
来源:互联网 发布:wap手机图文列表源码 编辑:程序博客网 时间:2024/05/16 06:03
HashMap
允许设置key和value为null,key存放是乱序的,不支持线程的同步,即任一时刻可以有多个线程同时写HashMap;可能会导致数据的不一致。如果需要同步,可以用 Collections的synchronizedMap方法使HashMap具有同步的能力,或者使用ConcurrentHashMap,访问速度快,因为它根据key的HashCode 值来存储数据
public static void main(String[] args) {System.out.println("************************* HashMap *******************************");HashMap<String, String> hashMap = new HashMap<String, String>();hashMap.put("X-rapido", "Admin");hashMap.put("X-rapido", "User"); // 重复的会替换旧数据hashMap.put("Y-miya", null);hashMap.put("J-lina", null);System.out.println(hashMap.get(null));hashMap.put(null, "Manager");hashMap.put(null, "liner");hashMap.put("X", "好帅");hashMap.put("PrettyBoy", "X-rapido");System.out.println(hashMap.get(null) + " : " + hashMap.get("刘仁奎") + "\n");for (String key : hashMap.keySet()) {System.out.println(key + " : " + hashMap.get(key));}}结果
************************* HashMap *******************************
null
liner : 好帅
null : liner
J-lina : null
Y-miya : null
X-rapido : User
PrettyBoy : X-rapido
X : 好帅
TreeMap
不允许key为null,但允许value为null,线程非同步,存放是根据key默认是升序排序,也可以指定排序的比较器,当用Iterator 遍历TreeMap时,得到的记录是排过序的。
public static void main(String[] args) {System.out.println("************************* TreeMap *******************************");TreeMap<String, String> treeMap = new TreeMap<String, String>();treeMap.put("X-rapido", "Admin");treeMap.put("X-rapido", "User"); // 重复的会替换旧数据treeMap.put("Y-miya", null);treeMap.put("J-lina", null);System.out.println(treeMap.get(null));treeMap.put(null, "Manager");}结果
************************* TreeMap *******************************
Exception in thread "main" java.lang.NullPointerException
at java.util.TreeMap.getEntry(TreeMap.java:324)
at java.util.TreeMap.get(TreeMap.java:255)
at com.founder.MapSoft.main(MapSoft.java:32)
正确代码
System.out.println("************************* TreeMap *******************************");TreeMap<String, String> treeMap = new TreeMap<String, String>();treeMap.put("X-rapido", "Admin");treeMap.put("X-rapido", "User"); // 重复的会替换旧数据treeMap.put("Y-miya", null);treeMap.put("J-lina", null);treeMap.put("X", "好帅");treeMap.put("PrettyBoy", "X-rapido");for (String key : treeMap.keySet()) {System.out.println(key+" : "+treeMap.get(key));}结果
************************* TreeMap *******************************
J-lina : null
PrettyBoy : X-rapido
X-rapido : User
Y-miya : null
X : 好帅
可以使用System.out.println(treeMap.firstEntry().getKey());和System.out.println(treeMap.firstKey());获取排完序之后的第一个key和value
Hashtable
key和value都不允许为null,线程同步,即任一时刻只有一个线程能写Hashtable,因此也导致了Hashtale在写入时会比较慢
LinkedHashMap
保存插入的顺序,线程非同步,在用Iterator遍历LinkedHashMap时,先得到的记录肯定是先插入的.在遍历的时候会比HashMap慢。key和value均允许为空,非同步的
不支持线程的同步,即任一时刻可以有多个线程同时写HashMap;可能会导致数据的不一致。如果需要同步,可以用 Collections的synchronizedMap方法使HashMap具有同步的能力,或者使用ConcurrentHashMap
实现类
Map
继承类
AbstractMap
Dictionary
AbstractMap
HashMap
名称
HashMap
HashTable
TreeMap
LinkedHashMap
是否有序排列
n(随机的)
n
y (默认升序)
y(按插入顺序排列)
线程是否同步
n
y
n
n
允许Key为null
y
n
n
y
允许Value为null
y
n
y
y
性能
快(HashMap的遍历速度和他的容量有关)
慢
块
慢(遍历速度只和实际数据有关,和容量无关)
应用范围
插入、删除和定位元素,HashMap是最好的选择
线程安全
取出来的是排序后的键值对,插入、删除需要维护平衡会牺牲一些效率
需要输出的顺序和输入的顺序相同
Java Map集合利用比较器Comparator根据Key和Value的排序
参考文章:http://ibloger.net/article/154.html
- HashMap,LinkedHashMap,HashTable,TreeMap 区别
- hashMap hashTable TreeMap linkedHashMap区别
- Hashtable ,TreeMap ,HashMap ,LinkedHashMap 区别
- HashMap、TreeMap、Hashtable、LinkedHashMap区别
- HashMap TreeMap Hashtable LinkedHashMap 区别
- HashMap,LinkedHashMap,TreeMap,HashTable区别
- HashMap Hashtable LinkedHashMap 和TreeMap区别
- HashMap,LinkedHashMap,Hashtable,TreeMap等区别
- JAVA HashMap,LinkedHashMap,TreeMap,HashTable 的区别
- HashMap、Hashtable、LinkedHashMap 和TreeMap区别
- HashMap、TreeMap、LinkedHashMap、hashtable的区别
- HashMap,LinkedHashMap,TreeMap,HashTable的区别
- HashMap Hashtable LinkedHashMap TreeMap的区别
- HashMap / Hashtable / LinkedHashMap / TreeMap的区别
- HashMap Hashtable LinkedHashMap和TreeMap区别
- HashMap、TreeMap、LinkedHashMap、hashtable的区别
- HashMap,HashTable,LinkedHashMap,TreeMap的区别
- HashMap,LinkedHashMap,TreeMap,HashTable的区别
- 删除数组零元素
- 在wamp集成环境php该如何开启短标签
- mysql 操作总结 INSERT和REPLACE
- linux学习(23)vi,vim的使用
- JAVA内部类使用,什么时候该使用内部类及使用内部类的好处
- HashMap、TreeMap、Hashtable、LinkedHashMap区别
- UVa #12569 Planning mobile robot on Tree (EASY Version) (习题7-11)
- Unity引擎制作仿《文明》游戏
- ubuntu系统用户密码忘记了怎么办
- time_t
- linux select与poll实现机制与实例分析
- 子进程模块subprocess
- CF~Good Bye 2014 B. New Year Permutation
- 五子棋Pro-最好玩的五子棋游戏