0%

HashSet 原理与源码分析

1、原理

2、源码分析

2.1、构造方法

1
2
3
4
//底层采用HashMap实现,只取HashMap存放key的一列
public HashSet() {
map = new HashMap<>();
}

2.2、其他方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//由于HashSet底层采用HashMap实现,所以HashSet底层同样采用HashMap的方法。
//HashSet只使用map中保存key的一列,PRESENT用来填充value一列的值
private static final Object PRESENT = new Object();
public int size() {
return map.size();
}
public boolean isEmpty() {
return map.isEmpty();
}
public boolean contains(Object o) {
return map.containsKey(o);
}
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
public boolean remove(Object o) {
return map.remove(o)==PRESENT;
}
public void clear() {
map.clear();
}

HashMap 原理与源码解析

1、原理

2、源码解析

2.1、构造方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public HashMap(int initialCapacity, float loadFactor) {
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
//容量最大为2的30次方,
if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
if (loadFactor <= 0 || Float.isNaN(loadFactor))
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
//设置扩容因子
this.loadFactor = loadFactor;
//设置大小,大于输入值的最小2的幂方
this.threshold = tableSizeFor(initialCapacity);
}

2.2、put 方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);
}
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
//判断数组结构是否存在,如不存在,建立并获取长度
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
//根据key的hash值判断key是否存在于数组中,不存在就放入,存在往下走去链表结构
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
//判断hash值是否匹配
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
//判断红黑树结构还是链表结构
else if (p instanceof TreeNode)
//数据放入红黑树结构
e = ((TreeNode<K,V>)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);
//链表长度大于8,结构转换为红黑树
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
//遇到相同的key,新数据覆盖旧数据
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
/如果是新数据覆盖旧数据,返回旧数据
if (e != null) { // existing mapping for key
V oldValue = e.value;
if (!onlyIfAbsent || oldValue == null)
e.value = value;
afterNodeAccess(e);
return oldValue;
}
}
//修改次数加一
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}

2.3、get 方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public V get(Object key) {
Node<K,V> e;
return (e = getNode(hash(key), key)) == null ? null : e.value;
}
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
//判断数据结构是否存在
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
//是数组中元素,即链表头节点,返回
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
//不是数组中元素,在其后的结构中(链表或红黑树)
if ((e = first.next) != null) {
//根据头节点,判断数据结构
if (first instanceof TreeNode)
//从红黑树中获取值
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
//遍历列表,获取值
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}

2.4、resize 方法(初始化数组结构或者扩容)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
final Node<K,V>[] resize() {
Node<K,V>[] 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 threshold
newCap = oldThr;
//原容量为0并且未设置下次扩容后大小
else { // zero initial threshold signifies using defaults
newCap = DEFAULT_INITIAL_CAPACITY;
//负载因子设置下次扩容后大小
newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
}
//设置下次扩容后大小为0的,重设下次扩容后大小
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<K,V>[] newTab = (Node<K,V>[])new Node[newCap];
table = newTab;
if (oldTab != null) {
//通过循环将扩容前数组的数据放入扩容后数组
for (int j = 0; j < oldCap; ++j) {
Node<K,V> 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<K,V>)e).split(this, newTab, j, oldCap);
//链表结构转移
else { // preserve order
Node<K,V> loHead = null, loTail = null;
Node<K,V> hiHead = null, hiTail = null;
Node<K,V> next;
do {
next = e.next;
if ((e.hash & oldCap) == 0) {
if (loTail == null)
loHead = e;
else
loTail.next = e;
loTail = e;
}
else {
if (hiTail == null)
hiHead = e;
else
hiTail.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;
}

2.5、remove 方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public V remove(Object key) {
Node<K,V> e;
return (e = removeNode(hash(key), key, null, false, true)) == null ?
null : e.value;
}
final Node<K,V> removeNode(int hash, Object key, Object value,
boolean matchValue, boolean movable) {
Node<K,V>[] tab; Node<K,V> p; int n, index;
//判断数据结构是否存在
if ((tab = table) != null && (n = tab.length) > 0 &&
//定位hash在数组结构中的位置
(p = tab[index = (n - 1) & hash]) != null) {
Node<K,V> node = null, e; K k; V v;
//是数组中数据,获取
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
node = p;
//不是数组中数据,在其后数据结构中查找
else if ((e = p.next) != null) {
//在红黑树中查找
if (p instanceof TreeNode)
node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
//在链表中查找
else {
do {
if (e.hash == hash &&
((k = e.key) == key ||
(key != null && key.equals(k)))) {
node = e;
break;
}
p = e;
} while ((e = e.next) != null);
}
}
//根据hash和key查找到数据,并且value匹配
if (node != null && (!matchValue || (v = node.value) == value ||
(value != null && value.equals(v)))) {
//从数据结构中移除节点
if (node instanceof TreeNode)
((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
else if (node == p)
tab[index] = node.next;
else
p.next = node.next;
//修改次数加一
++modCount;
//大小减一
--size;
afterNodeRemoval(node);
return node;
}
}
return null;
}

ArrayList 原理与源码解析

1、原理

2、源码解析

2.1、构造方法

1
2
3
4
5
6
7
8
9
10
11
12
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
//创建数组,根据传入大小
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
//创建空数组
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}

2.2、add 方法

1
2
3
4
5
6
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
//往数组中插入元素
elementData[size++] = e;
return true;
}

2.3、remove 方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public E remove(int index) {
rangeCheck(index);
modCount++;
//获取要删除的值
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
//要删除的位置后面的元素向前移动一位,覆盖掉要删除的值
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
return oldValue;
}
public boolean remove(Object o) {
if (o == null) {
//查找到要删除的值,再删除
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
//查找到要删除的值,再删除
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
//按照索引删除,底层与按索引删除方法相同
fastRemove(index);
return true;
}
}
return false;
}

问题

网络问题

参考的对象类型不支持尝试的操作

  这种问题一般是因为有启动过代理软件,造成代理冲突。

阅读全文 »

常用命令

修改mysql时区

1
2
3
4
5
6
7
8
9
# 查看时区
show variables like "%time_zone%";

# 设置时区为UTC+8:00
set global time_zone = '+8:00';
set time_zone = '+8:00';

# 刷新立即生效
flush privileges;
阅读全文 »

背景:

  项目中需要从两个不同的数据库查询数据,之前实现方法是:springboot配置连接一个数据源,另一个使用jdbc代码连接。
  为了改进,现在使用SpringBoot配置连接两个数据源。

阅读全文 »

介绍

  zookeeper是一个典型的发布/订阅模式的分布式数据管理与协调框架。

作用:

  1. 高性能使得ZooKeeper能够应用于对系统吞吐有明确要求的大型分布式系统。
  2. 高可用可以解决分布式的单点问题。
  3. 具有严格的顺序访问控制能力,主要是针对写操作的严格顺序性,使得客户端可以基于ZooKeeper来实现一些复杂的同步原语。
阅读全文 »

序列化与反序列化

  1. 序列化:把对象转换为字节数组的过程称为对象的序列化。
  2. 反序列化:把字节序列转换为对象的过程称为对象的反序列化。
阅读全文 »