Skip to content

Commit

Permalink
每次扩容,当前容量的两倍
Browse files Browse the repository at this point in the history
  • Loading branch information
jinchunzhao committed Oct 18, 2020
1 parent 0766766 commit d8ee736
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/main/java/com/github/jinchunzhao/JyHashMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class JyHashMap<K, V> implements JyMap<K, V>, Cloneable, Serializable {
transient Node<K, V>[] table;

public JyHashMap() {
threshold = (int) (DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
// threshold = (int) (DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
this.loadFactor = DEFAULT_LOAD_FACTOR;
}

Expand Down Expand Up @@ -217,13 +217,13 @@ final void putVal(int hash, K key, V value) {
Node<K,V>[] tab;
int n = 0;
//容器是否为空为空则初始化扩容。
if (table == null || table.length == 0) {
if ((tab = table) == null || (n = table.length) == 0) {
tab = resize();
n = tab.length;
}

//如果size大于阈值则进行扩容
if (size > threshold) {
if (size >= threshold) {
tab = resize();
n = tab.length;
}
Expand All @@ -235,7 +235,7 @@ final void putVal(int hash, K key, V value) {
Node<K, V> node = table[index];
if (Objects.isNull(node)) {
table[index] = newNode(hash, key, value, null);
if(++size > threshold){
if(++size >= threshold){
resize();
}
} else {
Expand Down Expand Up @@ -332,19 +332,20 @@ private Node<K, V>[] resize() {
threshold = Integer.MAX_VALUE;
return oldTab;
} else {
newCap = DEFAULT_INITIAL_CAPACITY;
//扩容两倍
newCap = oldCap << 1;
if (newCap < MAXIMUM_CAPACITY && oldCap >= DEFAULT_INITIAL_CAPACITY) {
//扩容两倍
newThr = oldThr << 1;
}else{
newCap = MAXIMUM_CAPACITY;
newThr = (int)(MAXIMUM_CAPACITY * DEFAULT_LOAD_FACTOR);
}
}
} else if (oldThr > 0){
newCap = oldThr;
} else {
newCap = DEFAULT_INITIAL_CAPACITY;
newThr = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
}
if(newThr == 0){
float ft = (float) newCap * loadFactor;
newThr = (newCap < MAXIMUM_CAPACITY && ft < (float) MAXIMUM_CAPACITY ? (int)ft : Integer.MAX_VALUE);
}

Node<K, V>[] newTab = (Node<K, V>[]) new Node[newCap];
table = newTab;
threshold = newThr;
Expand All @@ -354,6 +355,7 @@ private Node<K, V>[] resize() {
if ((e = oldTab[j]) != null) {
oldTab[j] = null;
if (e.next == null) {
// e.hash & (newCap - 1)
newTab[e.hash & (newCap - 1)] = e;
}
}
Expand All @@ -374,7 +376,9 @@ public void print() {
while (node != null) {
System.out.print("[ key:" + node.getKey() + ", value:" + node.getValue() + " ]");
node = node.next;

}
System.out.println();
}
System.out.println("-------------------打印结束-----------------------");
}
Expand Down

0 comments on commit d8ee736

Please sign in to comment.