hashmap与hashtable的区别,以及实现hashmap的同步操
2016-01-25 16:29
615 查看
Hashtable和HashMap的区别
1.Hashtable是Dictionary的子类,HashMap是Map接口的一个实现类;
2.Hashtable中的方法是同步的,而HashMap中的方法在缺省情况下是非同步的。即是说,在多线程应用程序中,不用专门的操作就安全地可以使用Hashtable了;而对于HashMap,则需要额外的同步机制。但HashMap的同步问题可通过Collections的一个静态方法得到解决:
Map Collections.synchronizedMap(Map m)
这个方法返回一个同步的Map,这个Map封装了底层的HashMap的所有方法,使得底层的HashMap即使是在多线程的环境中也是安全的。
3.在HashMap中,null可以作为键,这样的键只有一个;可以有一个或多个键所对应的值为null。当get()方法返回null值时,即可以表示HashMap中没有该键,也可以表示该键所对应的值为null。因此,在HashMap中不能由get()方法来判断HashMap中是否存在某个键,而应该用containsKey()方法来判断。
4.其底层的实现机制不同,hashmap的访问速度要快于hashtable,因为它不需要进行同步检验,建议在非多线程环境中使用hashmap代替hashtable .
HashMap与Hashtable的区别
HashTable的应用非常广泛,HashMap是新框架中用来代替HashTable的类,也就是说建议使用HashMap,不要使用HashTable。可能你觉得HashTable很好用,为什么不用呢?这里简单分析他们的区别。
1.HashTable的方法是同步的,HashMap未经同步,所以在多线程场合要手动同步HashMap这个区别就像Vector和ArrayList一样。
2.HashTable不允许null值(key和value都不可以),HashMap允许null值(key和value都可以)。
3.HashTable有一个contains(Object value),功能和containsValue(Object value)功能一样。
4.HashTable使用Enumeration,HashMap使用Iterator。
以上只是表面的不同,它们的实现也有很大的不同。
5.HashTable中hash数组默认大小是11,增加的方式是 old*2+1。HashMap中hash数组的默认大小是16,而且一定是2的指数。
6.哈希值的使用不同,HashTable直接使用对象的hashCode,代码是这样的:
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
而HashMap重新计算hash值,而且用与代替求模:
int hash = hash(k);
int i = indexFor(hash, table.length);
static int hash(Object x) {
int h = x.hashCode();
h += ~(h < < 9);
h ^= (h >>> 14);
h += (h < < 4);
h ^= (h >>> 10);
return h;
}
static int indexFor(int h, int length) {
return h & (length-1);
}
以上只是一些比较突出的区别,当然他们的实现上还是有很多不同的,比如
HashMap对null的操作
例子
package testMapAndTable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
public class TableMap {
public static void main(String... strings) {
Map<Integer, Integer> map = Collections
.synchronizedMap(new HashMap<Integer, Integer>());// 为hashmap加上同步
Hashtable<Integer, Integer> table = new Hashtable<Integer, Integer>();
long before = System.currentTimeMillis();
add(map);
System.out.println("add map time="
+ (System.currentTimeMillis() - before));
before = System.currentTimeMillis();
add(table);
System.out.println("add table time="
+ (System.currentTimeMillis() - before));
before = System.currentTimeMillis();
get(map);
System.out.println("get map time="
+ (System.currentTimeMillis() - before));
before = System.currentTimeMillis();
get(table);
System.out.println("get table time="
+ (System.currentTimeMillis() - before));
}
public static void add(Map<Integer, Integer> map) {
for (int i = 0; i < 300000; i++) {
map.put(i, i);
}
}
public static void get(Map<Integer, Integer> map) {
for (int i = 0; i < 300000; i++) {
map.get(i);
}
}
}
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(801) | 评论(0) | 转发(1) |
0
上一篇:【转】深入理解JDBC Scrollable ResultSet
下一篇:【转】MySQL命令大全
相关热门文章
HashMap根据value排序
android 获取第三方应用程序包...
Java中HashMap遍历的两种方式...
solrj 局部 更新 字段 field ...
Memcached
Tomcat 6 配置SSI
tomcat + ssi
JDK1.6官方下载_JDK6官方下载_...
Java 判断文件夹、文件是否存...
如何正确(完美)卸载Java/JDK/J...
linux dhcp peizhi roc
关于Unix文件的软链接
求教这个命令什么意思,我是新...
sed -e "/grep/d" 是什么意思...
谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
评论热议
1.Hashtable是Dictionary的子类,HashMap是Map接口的一个实现类;
2.Hashtable中的方法是同步的,而HashMap中的方法在缺省情况下是非同步的。即是说,在多线程应用程序中,不用专门的操作就安全地可以使用Hashtable了;而对于HashMap,则需要额外的同步机制。但HashMap的同步问题可通过Collections的一个静态方法得到解决:
Map Collections.synchronizedMap(Map m)
这个方法返回一个同步的Map,这个Map封装了底层的HashMap的所有方法,使得底层的HashMap即使是在多线程的环境中也是安全的。
3.在HashMap中,null可以作为键,这样的键只有一个;可以有一个或多个键所对应的值为null。当get()方法返回null值时,即可以表示HashMap中没有该键,也可以表示该键所对应的值为null。因此,在HashMap中不能由get()方法来判断HashMap中是否存在某个键,而应该用containsKey()方法来判断。
4.其底层的实现机制不同,hashmap的访问速度要快于hashtable,因为它不需要进行同步检验,建议在非多线程环境中使用hashmap代替hashtable .
HashMap与Hashtable的区别
HashTable的应用非常广泛,HashMap是新框架中用来代替HashTable的类,也就是说建议使用HashMap,不要使用HashTable。可能你觉得HashTable很好用,为什么不用呢?这里简单分析他们的区别。
1.HashTable的方法是同步的,HashMap未经同步,所以在多线程场合要手动同步HashMap这个区别就像Vector和ArrayList一样。
2.HashTable不允许null值(key和value都不可以),HashMap允许null值(key和value都可以)。
3.HashTable有一个contains(Object value),功能和containsValue(Object value)功能一样。
4.HashTable使用Enumeration,HashMap使用Iterator。
以上只是表面的不同,它们的实现也有很大的不同。
5.HashTable中hash数组默认大小是11,增加的方式是 old*2+1。HashMap中hash数组的默认大小是16,而且一定是2的指数。
6.哈希值的使用不同,HashTable直接使用对象的hashCode,代码是这样的:
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
而HashMap重新计算hash值,而且用与代替求模:
int hash = hash(k);
int i = indexFor(hash, table.length);
static int hash(Object x) {
int h = x.hashCode();
h += ~(h < < 9);
h ^= (h >>> 14);
h += (h < < 4);
h ^= (h >>> 10);
return h;
}
static int indexFor(int h, int length) {
return h & (length-1);
}
以上只是一些比较突出的区别,当然他们的实现上还是有很多不同的,比如
HashMap对null的操作
例子
package testMapAndTable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
public class TableMap {
public static void main(String... strings) {
Map<Integer, Integer> map = Collections
.synchronizedMap(new HashMap<Integer, Integer>());// 为hashmap加上同步
Hashtable<Integer, Integer> table = new Hashtable<Integer, Integer>();
long before = System.currentTimeMillis();
add(map);
System.out.println("add map time="
+ (System.currentTimeMillis() - before));
before = System.currentTimeMillis();
add(table);
System.out.println("add table time="
+ (System.currentTimeMillis() - before));
before = System.currentTimeMillis();
get(map);
System.out.println("get map time="
+ (System.currentTimeMillis() - before));
before = System.currentTimeMillis();
get(table);
System.out.println("get table time="
+ (System.currentTimeMillis() - before));
}
public static void add(Map<Integer, Integer> map) {
for (int i = 0; i < 300000; i++) {
map.put(i, i);
}
}
public static void get(Map<Integer, Integer> map) {
for (int i = 0; i < 300000; i++) {
map.get(i);
}
}
}
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(801) | 评论(0) | 转发(1) |
0
上一篇:【转】深入理解JDBC Scrollable ResultSet
下一篇:【转】MySQL命令大全
相关热门文章
HashMap根据value排序
android 获取第三方应用程序包...
Java中HashMap遍历的两种方式...
solrj 局部 更新 字段 field ...
Memcached
Tomcat 6 配置SSI
tomcat + ssi
JDK1.6官方下载_JDK6官方下载_...
Java 判断文件夹、文件是否存...
如何正确(完美)卸载Java/JDK/J...
linux dhcp peizhi roc
关于Unix文件的软链接
求教这个命令什么意思,我是新...
sed -e "/grep/d" 是什么意思...
谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
评论热议
相关文章推荐
- 【转】深入理解JDBC Scrollable ResultSet
- Thunderbird使用163邮箱smtp设置方法
- jdk环境变量配置:java.lang.NoClassDefFoundError
- 两种方式调试JNI中DLL(动态链接库)
- 【转】Java命令行运行参数说明大全(偷来的)
- 【转】开源日志库<log4cplus+VS2008使用>整理
- Opera邮件客户端设置
- 网络的7层协议
- ViewPager 的 PagerAdapter的官方的说明 摘录
- 第三方支付宝支付
- Redis学习笔记6--Redis事务
- ARM TrustZone
- HashMap的工作原理
- jdk环境变量配置:java.lang.NoClassDefFoundError
- 两种方式调试JNI中DLL(动态链接库)
- 【转】Java命令行运行参数说明大全(偷来的)
- 【转】开源日志库<log4cplus+VS2008使用>整理
- Opera邮件客户端设置
- Thunderbird使用163邮箱smtp设置方法
- 【转】深入理解JDBC Scrollable ResultSet