首先,让我们先从最简单的开始,总的来说,hashSet可以说是建立在hashMap上的变种应用。
通过阅读hashSet的源码我们可以得出以下结论:
1. hashSet的add方法,是巧妙运用了hashMap的key值不可重复的特性;
2. 由于hashMap的可以是可以为null的,所以,hashSet的值支持为null;
3. hashSet不是线程安全的;
4. hashSet的clone方法,是浅复制,即当原set内的元素值改变时,copy的set里的值也改变;
5. 当不指定容量时,hashSet的默认容量时16,负载因子是0.75;
6. 由于hashSet的iterator(迭代器)是fail-fast模式,因此在interator操作时,hashSet发生变化,会导致异常抛出: ConcurrentModificationException;
由于hashMap的源码比较长,我们就不在这里贴出了,通过阅读hashSet的源码我们可以得出以下结论:
**1. 默认容量是16,最大容量是2^30,默认负载因子0.75,自定义容量时必须是2的幂级;
linkedHashMap是jdk1.4出现的,是java 集合框架的一员;是hashMap的子类。通过源码阅读,可以获取一下信息:
1. 其结构是双向链表,对hashMap.node做了拓展,通过before和after记录相邻数据;
3. 用head和tail记录最早的和最新的数据;
4. 其默认容量,负载因子等,同hashMap;
5. 多了个accessOrder变量,用于遍历时的输出顺序,默认false,即以插入顺序遍历;
6. 由于linkedHashMap是hashMap的子类,它的树化逻辑同hashMap;
7. 它不是线程安全的;
阅读hashTable的源码,可知:
ConcurrentHashMap是针对hashMap的安全性做的设计和拓展,
因此它具有hashMap的很多特性,比如容量、负载因子、树化等,除此之外,为了保证其线程安全性和拓展性能,ConcurrentHashMap在存储结构上也做了一些改造,通过阅读源码,可以得到以下几点:
/* ---------------- Fields -------------- *//*** The array of bins. Lazily initialized upon first insertion.* Size is always a power of two. Accessed directly by iterators.*/transient volatile Node[] table;/*** The next table to use; non-null only while resizing.*/private transient volatile Node[] nextTable;/*** Base counter value, used mainly when there is no contention,* but also as a fallback during table initialization* races. Updated via CAS.*/private transient volatile long baseCount;/*** Table initialization and resizing control. When negative, the* table is being initialized or resized: -1 for initialization,* else -(1 + the number of active resizing threads). Otherwise,* when table is null, holds the initial table size to use upon* creation, or 0 for default. After initialization, holds the* next element count value upon which to resize the table.*/private transient volatile int sizeCtl;/*** The next table index (plus one) to split while resizing.*/private transient volatile int transferIndex;/*** Spinlock (locked via CAS) used when resizing and/or creating CounterCells.*/private transient volatile int cellsBusy;/*** Table of counter cells. When non-null, size is a power of 2.*/private transient volatile CounterCell[] counterCells;// viewsprivate transient KeySetView keySet;private transient ValuesView values;private transient EntrySetView entrySet;