简单谈hashmap hashtable linkedhashmap treemap的区别

来源:互联网 发布:腾讯视频播放器mac 编辑:程序博客网 时间:2024/06/07 15:53

hashmap

继承dictionary;线程不同步。如果想使用同步可以使用collections.syschronizedMap或者concurrentHashMap进行同步,允许一个键为null,随机读取数据,读取速度快

 @Testpublic void testHashMap() {HashMap<Integer, String> map = new HashMap<Integer, String>();// 在map中存入10000个键值对long start = System.currentTimeMillis();for (int i = 0; i < 100000; i++) {map.put(i, "hashvalue " + i);}long end = System.currentTimeMillis();long insertTime=end-start;start = System.currentTimeMillis();// 输出结果for (Integer key : map.keySet()) {System.out.println("key:" + key.valueOf(key) + "  value:"+ map.get(key));}end = System.currentTimeMillis();System.out.println("size:" + map.size());System.out.println("输入花费总时间:" + (insertTime));System.out.println("输出花费总时间:" + (end - start));map.clear();map.put(null, "测试键可以为null");System.out.println(map.get(null));}

结论:

从程序结果中可以看出,读取的数据是随机的。一个键可以为null

hashtable

继承dictionary;线程同步。不允许为null, 

@Testpublic void testHashtable() {Hashtable<Integer, String> map = new Hashtable<Integer, String>();// 在map中存入10000个键值对long start = System.currentTimeMillis();for (int i = 0; i < 100000; i++) {map.put(i, "hashvalue " + i);}long end = System.currentTimeMillis();long insertTime=end-start;start = System.currentTimeMillis();// 输出结果for (Integer key : map.keySet()) {System.out.println("key:" + key.valueOf(key) + "  value:"+ map.get(key));}end = System.currentTimeMillis();System.out.println("size:" + map.size());System.out.println("输入花费总时间:" + (insertTime));System.out.println("输出花费总时间:" + (end - start));map.clear();map.put(null, "测试键可以为null");System.out.println(map.get(null));}

结论:

读取数据使用散列方式排列,键不能为空,编译可以通过。运行会包空指针异常

linkedhashmap

保存了记录的插入顺序,也是按顺序排列的,


@Testpublic void testLinkedHashMap() {LinkedHashMap<Integer, String> map = new LinkedHashMap<Integer, String>();// 在map中存入10000个键值对long start = System.currentTimeMillis();for (int i = 0; i < 100000; i++) {map.put(i, "hashvalue " + i);}long end = System.currentTimeMillis();long insertTime = end - start;start = System.currentTimeMillis();// 输出结果for (Integer key : map.keySet()) {System.out.println("key:" + key.valueOf(key) + "  value:"+ map.get(key));}end = System.currentTimeMillis();System.out.println("size:" + map.size());System.out.println("输入花费总时间:" + (insertTime));System.out.println("输出花费总时间:" + (end - start));map.clear();map.put(null, "测试键可以为null");System.out.println(map.get(null));}

结论:

Linkedhashmap输出的顺序和输入的相同按顺序排列,Linkedhashmap继承hashmap 所有他的键可以有一个值为null,

treemap

采用的是红黑树算法的实现。按自然排序的升序进行排序。

@Testpublic void testTreeMap() {TreeMap<Integer, String> map = new TreeMap<Integer, String>();// 在map中存入10000个键值对long start = System.currentTimeMillis();for (int i = 0; i < 100000; i++) {map.put(i, "hashvalue " + i);}long end = System.currentTimeMillis();long insertTime = end - start;start = System.currentTimeMillis();// 输出结果for (Integer key : map.keySet()) {System.out.println("key:" + key.valueOf(key) + "  value:"+ map.get(key));}end = System.currentTimeMillis();System.out.println("size:" + map.size());System.out.println("输入花费总时间:" + (insertTime));System.out.println("输出花费总时间:" + (end - start));map.clear();map.put(null, "测试键可以为null");System.out.println(map.get(null));}



Treemap的键不能为null 查询出来的数据是按顺序排列输出


0 0
原创粉丝点击