JAVA集合类中的哈希总结
目 录
1、哈希表
2、Hashtable、HashMap、ConcurrentHashMap、LinkedHashMap、TreeMap区别
3、Hashtable、HashMap、ConcurrentHashMap、LinkedHashMap、TreeMap源码分析
4、一致性哈希算法
5、transient使用方法
6、迭代器的强一致和弱一致
7、总结
一、哈希表
哈希表,是一种数据结构。它通过把关键码值映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。
常用的散列函数方法有:取余数法、平方取中法、线性函数法、随机数法等。常见的解决冲突的方法有:链地址法、开发定址法、建立公共溢出区、多哈希函数法。
Java中的哈希表,即类Hashtable。它的散列方法采用了除留取余数法;解决冲突的方法采用了链地址法。链地址法使用于频繁的插入和删除的操作类型。
二、Hashtable、HashMap、ConcurrentHashMap、LinkedHashMap、TreeMap区别
Hashtable是一个包含单向链表的二维数组,其数据结构的数组中是Entry<K,V>存储,entry对象。Hashtable有洁癖,不允许存入其中的key或者value为null。Hashtable是线程安全的,所有的方法均用synchronized修饰,这样在任一时刻,只有一个线程可以写Hashtable,因此,对于频繁写操作的业务逻辑,诸如写excel表等时候,速度会非常慢。
HashMap是最常用的Map型数据结构,它根据键的hashCode()值存储数据。HashMap允许一个key为null,允许多个value为空,HashMap不支持线程的同步,即可能会出现在同一时刻有多个线程同时写HashMap,会产生数据的不一致。如果在修改代码的过程中,需要给HashMap限制为线程同步的,可以采用Collections.synchronizedMap(map);方法使得HashMap可以同步。
ConcurrentHashMap是基于这样的考虑:降低锁的粒度。在Hashtable中的关键字是使用synchronized基于整张表结构的,锁的粒度太大,它每次通过锁住整张表让线程独占,来保证安全性。
LinkedHashMap保存了记录的插入顺序,在使用Iterator遍历LinkedHashMap的时候,先得到的记录肯定是先插入的。在遍历的时候会比HashMap慢,因为HashMap是以O(1)来设计存取的。并且LinkedHashMap继承自HashMap,拥有它的全部特性。
TreeMap是基于红黑树实现的,它是一种有序的存储结构,并且程序员可以自己定义排序器。TreeMap默认会按存入的键值key来排序,默认是按升序排序,当然也可以指定排序的比较器。TreeMap同样有洁癖,不允许存入null值。使用Iterator遍历出来的TreeMap往往是有序的。
总结:常用HashMap,允许null插入;有两个子类:ConcurrentHashMap和LinkedHashMap。前者用来弥补线程安全,后者用来弥补有序。此外还有Hashtable和TreeMap。虽然CouncurrentHashMap性能明显优于Hashtable,但是并不能完全取代Hashtable,因为遍历ConcurrentHashMap的迭代器是弱一致的。TreeMap数据结构则可以帮助我们得到一个有序的结果,适用于需要输出排序结果的场景。
三、Hashtable、HashMap、ConcurrentHashMap、LinkedHashMap、TreeMap源码分析
Hashtable源码如下:
public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, java.io.Serializable { /** * The hash table data. */ private transient Entry<K,V>[] table; /** * The total number of entries in the hash table. */ private transient int count; public Hashtable(int initialCapacity) { this(initialCapacity, 0.75f); } /** * Constructs a new, empty hashtable with the specified initial * capacity and the specified load factor. * * @param initialCapacity the initial capacity of the hashtable. * @param loadFactor the load factor of the hashtable. * @exception IllegalArgumentException if the initial capacity is less * than zero, or if the load factor is nonpositive. */ public Hashtable(int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); if (loadFactor <= 0 || Float.isNaN(loadFactor)) throw new IllegalArgumentException("Illegal Load: "+loadFactor); if (initialCapacity==0) initialCapacity = 1; this.loadFactor = loadFactor; table = new Entry[initialCapacity]; threshold = (int)Math.min(initialCapacity * loadFactor, MAX_ARRAY_SIZE + 1); initHashSeedAsNeeded(initialCapacity); } /** * Constructs a new, empty hashtable with the specified initial capacity * and default load factor (0.75). * * @param initialCapacity the initial capacity of the hashtable. * @exception IllegalArgumentException if the initial capacity is less * than zero. */ public Hashtable(int initialCapacity) { this(initialCapacity, 0.75f); } /** * Constructs a new, empty hashtable with a default initial capacity (11) * and load factor (0.75). */ public Hashtable() { this(11, 0.75f); } /** * Constructs a new hashtable with the same mappings as the given * Map. The hashtable is created with an initial capacity sufficient to * hold the mappings in the given Map and a default load factor (0.75). * * @param t the map whose mappings are to be placed in this map. * @throws NullPointerException if the specified map is null. * @since 1.2 */ public Hashtable(Map<? extends K, ? extends V> t) { this(Math.max(2*t.size(), 11), 0.75f); putAll(t); } /** * Returns the number of keys in this hashtable. * * @return the number of keys in this hashtable. */ public synchronized int size() { return count; } /** * Tests if this hashtable maps no keys to values. * * @return <code>true</code> if this hashtable maps no keys to values; * <code>false</code> otherwise. */ public synchronized boolean isEmpty() { return count == 0; }