您的位置:首页 > 其它

简单谈hashmap hashtable linkedhashmap treemap的区别

2016-08-30 19:48 543 查看
hashmap

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

@Test
public 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, 

@Test
public 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

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

@Test
public 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

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

@Test
public 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 查询出来的数据是按顺序排列输出
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息