`
iloveflower
  • 浏览: 76694 次
社区版块
存档分类
最新评论
  • iloveflower: 呵呵。好好学习。。。。。。。。。。。。
    java 读书
  • Eric.Yan: 看了一点,不过是电子版的……你这一说到提醒我了,还要继续学习哈 ...
    java 读书

Difference between HashMap and HashTable? Can we make hashmap synchronized?

 
阅读更多
This question oftenly asked in interview to check whether candidate understand correct usage of collection classes and aware of alternative solutions available.

1. The HashMap class is roughly equivalent to Hashtable, except that it is non synchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable doesn't allow nulls).

2. HashMap does not guarantee that the order of the map will remain constant over time.

3. HashMap is non synchronized whereas Hashtable is synchronized.

4. Iterator in the HashMap is  fail-fast  while the enumerator for the Hashtable is not and throw ConcurrentModificationException if any other Thread modifies the map structurally  by adding or removing any element except Iterator's own remove()  method. But this is not a guaranteed behavior and will be done by JVM on best effort.

Note on Some Important Terms

1)Synchronized means only one thread can modify a hash table at one point of time. Basically, it means that any thread before performing an update on a hashtable will have to acquire a lock on the object while others will wait for lock to be released.

2)Fail-safe is relevant from the context of iterators. If an iterator has been created on a collection object and some other thread tries to modify the collection object "structurally", a concurrent modification exception wjavascript:void(0)ill be thrown. It is possible for other threads though to invoke "set" method since it doesn't modify the collection "structurally". However, if prior to calling "set", the collection has been modified structurally, "IllegalArgumentException" will be thrown.

3)Structurally modification means deleting or inserting element which could effectively change the structure of map.

HashMap can be synchronized by

Map m = Collections.synchronizeMap(hashMap);

Hope this will be useful.

java中HashMap,LinkedHashMap,TreeMap,HashTable的区别
java为数据结构中的映射定义了一个接口java.util.Map;它有四个实现类,分别是HashMap Hashtable LinkedHashMap 和TreeMap
Map主要用于存储健值对,根据键得到值,因此不允许键重复(重复了覆盖了),但允许值重复。
Hashmap 是一个最常用的Map,它根据键的HashCode 值存储数据,根据键可以直接获取它的值,具有很快的访问速度,遍历时,取得数据的顺序是完全随机的。HashMap最多只允许一条记录的键为Null;允许多条记录的值为 Null;HashMap不支持线程的同步,即任一时刻可以有多个线程同时写HashMap;可能会导致数据的不一致。如果需要同步,可以用 Collections的synchronizedMap方法使HashMap具有同步的能力,或者使用ConcurrentHashMap。
Hashtable与 HashMap类似,它继承自Dictionary类,不同的是:它不允许记录的键或者值为空;它支持线程的同步,即任一时刻只有一个线程能写Hashtable,因此也导致了 Hashtable在写入时会比较慢。
LinkedHashMap保存了记录的插入顺序,在用Iterator遍历LinkedHashMap时,先得到的记录肯定是先插入的.也可以在构造时用带参数,按照应用次数排序。在遍历的时候会比HashMap慢,不过有种情况例外,当HashMap容量很大,实际数据较少时,遍历起来可能会比LinkedHashMap慢,因为LinkedHashMap的遍历速度只和实际数据有关,和容量无关,而HashMap的遍历速度和他的容量有关。
TreeMap实现SortMap接口,能够把它保存的记录根据键排序,默认是按键值的升序排序,也可以指定排序的比较器,当用Iterator 遍历TreeMap时,得到的记录是排过序的。

一般情况下,我们用的最多的是HashMap,HashMap里面存入的键值对在取出的时候是随机的,它根据键的HashCode值存储数据,根据键可以直接获取它的值,具有很快的访问速度。在Map 中插入、删除和定位元素,HashMap 是最好的选择。
TreeMap取出来的是排序后的键值对。但如果您要按自然顺序或自定义顺序遍历键,那么TreeMap会更好。
LinkedHashMap 是HashMap的一个子类,如果需要输出的顺序和输入的相同,那么用LinkedHashMap可以实现,它还可以按读取顺序来排列,像连接池中可以应用。
参考资料:http://woshixushigang.iteye.com/blog/962587
分享到:
评论

相关推荐

    hashmap和hashtable的区别.docx

    1. HashMap几乎可以等价于Hashtable,除了HashMap是非synchronized的,并可以接受null(HashMap可以接受为null的键值(key)和值(value),而Hashtable则不行)。 2. HashMap是非synchronized,而Hashtable是synchronized...

    有关hashMap跟hashTable的区别,说法正确的是?

    HashMap是非synchronized,而Hashtable是synchronized C. HashTable使用Enumeration,HashMap使用Iterator D. HashMap允许将 null 作为一个 entry 的 key 或者 value,而 Hashtable 不允许。 答案 A B C D 解析 A...

    HashMap和Hashtable的区别

    Hashmap是开发中用的比较多的一种集合,是线程不安全的,Hashtable的方法上大多都加了synchronized所以是线程安全的,所以效率并不高。这也是它们最大的不同。  2.Hashtable继承的类和实现的接口:(Dictionary类...

    HashMap原理.docx

    HashMap是一个散列桶(数组和链表),它存储的内容是键值对(key-value)映射HashMap采用了数组和链表的数据结构,能在查询和修改方便继承了数组的线性查找和链表的寻址修改HashMap是非synchronized,所以HashMap很快...

    JavaSE 笔试 精华

    Map HashMap Dictionary Hashtable Comparetor 2. Vector和ArrayList、LinkedList区别? Hashtable 和 HashMap之间的区别 LinkedList内部以链表形式存储数据。 ArrayList内部以数组形式存储数据。 ? Vector同...

    大数据面试题.pdf

    如果是对其它指定位置的插⼊、删除操作,最好选择 LinkedList HashMap、HashTable 的区别及其优缺点: HashTable 中的⽅法是同步的 HashMap 的⽅法在缺省情况下是⾮同步的 因此在多线程环境下需要做额外的同步机制。...

    史上最全java面试,103项重点知识,带目录

    21. HashMap 和 Hashtable 有什么区别? 10 22. 如何决定使用 HashMap 还是 TreeMap? 10 23. 说一下 HashMap 的实现原理? 10 24. 说一下 HashSet 的实现原理? 11 25. ArrayList 和 LinkedList 的区别是什么? 11 ...

    Java面试题.docx

    29、HashMap和HashTable的区别 30、HashMap与HashSet的区别 31-40题 31、HashSet与HashMap怎么判断集合元素重复? 33、ArrayList和LinkedList的区别,以及应用场景 34、数组和链表的区别 35、开启线程的三种...

    java7hashmap源码-AndroidOffer:只为帮助您获得更好的报价

    对比:Hashtable、HashMap、LinkedHashMap、ConcurrentHashMap、TreeMap (看第六条就可以) HashMap 用什么数据结构实现的 加载因子是什么 HashMap 初始化传入的容量参数的值是就是 HashMap 实际分配的空间么 ...

    Java容器类List、ArrayList、Vector及map、HashTable应用

    List、ArrayList、Vector及map、HashTable、HashMap的区别与用法 使用容器排序 Vector由于使用了synchronized方法(线程安全)

    Java常见面试题208道.docx

    21.HashMap 和 Hashtable 有什么区别? 22.如何决定使用 HashMap 还是 TreeMap? 23.说一下 HashMap 的实现原理? 24.说一下 HashSet 的实现原理? 25.ArrayList 和 LinkedList 的区别是什么? 26.如何实现数组和 ...

    千方百计笔试题大全

    32、abstract 的method 是否可同时是static,是否可同时是native,是否可同时是synchronized? 11 33、静态变量和实例变量的区别? 11 34、是否可以从一个static 方法内部发出对非static 方法的调用? 11 35、写clone...

    java面试宝典

    32、abstract 的method 是否可同时是static,是否可同时是native,是否可同时是synchronized? 11 33、静态变量和实例变量的区别? 11 34、是否可以从一个static 方法内部发出对非static 方法的调用? 11 35、写clone...

    完美世界大数据笔试.pdf

    hashmap,hashtable区别 9. ArrayList,vector,linkedlist区别 10. synchronized和volatile区别 11. 接⼝与抽象类的区别 12. jvm垃圾回收机制 13. Annotation关键字 14. 竟然还有后缀式这种东西 15. 编程题⼀道...

    涵盖了90%以上的面试题

    如果HashMap或者hashTable的key是一个自定义的类该怎么办 为什么重写equals还要重写hashCode? 介绍一下volatile jdk1.5新特性 jdk1.7新特性 jdk1.8新特性 java语言有哪些优点? 同一个.java文件中是否可以有多个main...

    变态级JAVA程序员面试32问

     第五,HashMap和Hashtable的区别。  第六,Collection 和 Collections的区别。  第七,什么时候用assert.  第八,GC是什么? 为什么要有GC?  第九,String s = new String("xyz");创建了几个String ...

    xmljava系统源码-spark-notes:在编写spark或scala时注意任何事情

    xml java系统源码 ...HashMap和HashTable有什么区别? 请说一下你对Java关键字 synchronized 的理解,如果你不懂Java语言的话,也请尝试说说你对 synchronized 的理解 Java为什么会推出Java NIO(N

    关于JAVA面试的100题及其答案

    HashMap是Hashtable的轻量级实现(非线程安全的实现),他们都完成了Map接口,主要区别在于HashMap允许空(null)键值(key),由于非线程安全,效率上可能高于Hashtable。 HashMap允许将null作为一个entry的key或者...

    Java理论与实践:并发集合类

    本文介绍了在Java类库中出现的第一个关联的...Hashtable的后继者HashMap是作为JDK1.2中的集合框架的一部分出现的,它通过提供一个不同步的基类和一个同步的包装器Collections.synchronizedMap,解决了线程安全性问题。

Global site tag (gtag.js) - Google Analytics