前言:
本文主要是通过源码来解读一些自己还不懂的地方,一些数据结构上的东西,不做过多的解读。
容器总的来说分为两大类:
1、Collection:
存放的是单建元素(就是单个元素)
2、Map
存放的键值对(以Key-Value的方式存在)
List定义的模板方法如下:
底层的数据结构是数组,所以使用idx
学习重点在于扩容(add方法中很重要):
// 测试代码
public class ListTest {public static void main(String[] args) {List
1、使用默认的构造函数
2、调用add方法
其实看了源码可以知道:
Vector底层也一个数组,基本上和ArrayList一直
通过看源码,我们可以知道Vector是通过对每个方法添加synchronized关键字来保证这是一个线程安全的容器。
那么这样会使我们再使用这些方法时效率不高。
所以我们再日常开发中,很少使用他,但是再有竞争的情况下可以使用一下【其实使用也不是特别多,我们都是通过自己去完成的多】
1、底层是一个双向链表的数据结构
源码是定义了一个node节点,来存放前驱、后驱和元素
他的CRUD也是比较简单,主要就是基于双向链表的CRUD,要考虑好前驱指针和后驱指针指向的地址,就能弄明白。
总体add流程如下图:
HashSet
1)构造方法
构造方法也很清楚就是,hashMap
2)add方法
2.1 向hashMap中put数据,key就是我们add进来的值,Value是共享的一个填充对象
填充对像
===========>这里往下就是HashMap的东西咯,小东西真面目流露了
2.2 再往下面调用Map中的put方法了
2.3 将hashCode进行运算
将hashcode进行高16位和低16位进行异或运算(这样做的目的就是尽量的避免哈希冲突)==》得到hash值
/*** Implements Map.put and related methods** @param hash hash for key* @param key the key* @param value the value to put* @param onlyIfAbsent if true, don't change existing value* @param evict if false, the table is in creation mode.* @return previous value, or null if none*/final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {// 临时辅助遍历Node[] tab; Node p; int n, i;// 当定义的属性,数组位null 或者长队为0时调用resize方法进行长度的重置(下面的第二段代码注释)if ((tab = table) == null || (n = tab.length) == 0)n = (tab = resize()).length;// 使用n-1&hash计算下标值(数组长度&hash)if ((p = tab[i = (n - 1) & hash]) == null)tab[i] = newNode(hash, key, value, null);else {Node e; K k;if (p.hash == hash &&((k = p.key) == key || (key != null && key.equals(k))))e = p;else if (p instanceof TreeNode)e = ((TreeNode)p).putTreeVal(this, tab, hash, key, value);else {for (int binCount = 0; ; ++binCount) {if ((e = p.next) == null) {p.next = newNode(hash, key, value, null);if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1sttreeifyBin(tab, hash);break;}if (e.hash == hash &&((k = e.key) == key || (key != null && key.equals(k))))break;p = e;}}if (e != null) { // existing mapping for keyV oldValue = e.value;if (!onlyIfAbsent || oldValue == null)e.value = value;afterNodeAccess(e);return oldValue;}}++modCount;if (++size > threshold)resize();afterNodeInsertion(evict);return null;}
// 注意:阈值
/*** Initializes or doubles table size. If null, allocates in* accord with initial capacity target held in field threshold.* Otherwise, because we are using power-of-two expansion, the* elements from each bin must either stay at same index, or move* with a power of two offset in the new table.** @return the table*/final Node[] resize() {// 定义老的数组Node[] oldTab = table;int oldCap = (oldTab == null) ? 0 : oldTab.length;int oldThr = threshold;int newCap, newThr = 0;if (oldCap > 0) {// 必须是二次幂if (oldCap >= MAXIMUM_CAPACITY) {threshold = Integer.MAX_VALUE;return oldTab;}else if ((newCap = oldCap << 1) < MAXIMUM_CAPACITY &&oldCap >= DEFAULT_INITIAL_CAPACITY)newThr = oldThr << 1; // double threshold}else if (oldThr > 0) // initial capacity was placed in thresholdnewCap = oldThr;else { // zero initial threshold signifies using defaults// 这里是初次使用的默认值是16newCap = DEFAULT_INITIAL_CAPACITY;// 这是给扩容值进行计算(阈值是0.75*默认长度)//【这里之所以使用阈值计算扩容值,是为了防止并情况下扩容的问题假设你到满了才扩容,比如还剩4个位置,刚好同时来了5个,那怎么办呢】newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);}if (newThr == 0) {float ft = (float)newCap * loadFactor;newThr = (newCap < MAXIMUM_CAPACITY && ft < (float)MAXIMUM_CAPACITY ?(int)ft : Integer.MAX_VALUE);}threshold = newThr;@SuppressWarnings({"rawtypes","unchecked"})Node[] newTab = (Node[])new Node[newCap];table = newTab;if (oldTab != null) {for (int j = 0; j < oldCap; ++j) {Node e;if ((e = oldTab[j]) != null) {oldTab[j] = null;if (e.next == null)newTab[e.hash & (newCap - 1)] = e;else if (e instanceof TreeNode)((TreeNode)e).split(this, newTab, j, oldCap);else { // preserve orderNode loHead = null, loTail = null;Node hiHead = null, hiTail = null;Node next;do {next = e.next;if ((e.hash & oldCap) == 0) {if (loTail == null)loHead = e;elseloTail.next = e;loTail = e;}else {if (hiTail == null)hiHead = e;elsehiTail.next = e;hiTail = e;}} while ((e = next) != null);if (loTail != null) {loTail.next = null;newTab[j] = loHead;}if (hiTail != null) {hiTail.next = null;newTab[j + oldCap] = hiHead;}}}}}return newTab;}