• 如果您觉得本站非常有看点，那么赶紧使用Ctrl+D 收藏吧

# 关于数据结构

1个月前 (10-27) 17次浏览

## 数据结构

### 数组

``````char[] arrays = {'a','p','p','l','e',' ',' ','1','3','6','6','8','5','\$'}
``````

### HashMap

#### 存值

``````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;
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> 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<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);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(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 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;
}

``````

#### 哈希值

hash值计算是一种获取不重复值的算法，通常key不同，返回的数值不同

``````static final int hash(Object key) {
int h;
return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16);
}

public native int hashCode(); //jvm本地方法
``````

#### 索引算法

``````putVal(hash(key), key, value, false, true);

// 实际存值代码
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
// i = (n - 1) & hash   i就是计算出的索引值
``````

#### 快速查找

HashMap类似数组的快速定位查找与哈希计算和数组有关

``````/**
* The table, initialized on first use, and resized as
* necessary. When allocated, length is always a power of two.
* (We also tolerate length zero in some operations to allow
* bootstrapping mechanics that are currently not needed.)
*/
transient Node<K,V>[] table;
``````

table 是一个数组，数组元素的机构是Node

``````/**
* Basic hash bin node, used for most entries.  (See below for
* TreeNode subclass, and in LinkedHashMap for its Entry subclass.)
*/
static class Node<K,V> implements Map.Entry<K,V> {
final int hash;
final K key;
V value;
Node<K,V> next;  //指向像一个元素，是一种链表结构的应用

//... 省略一些代码
}
``````

Node 是类似链表结构，成员属性包含一个Node<K,V> next 有next是防止hash碰撞等情况

get方法获取值（查找

``````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) {
//判断如果table不是空  取值 tab[(n - 1) & hash]
//可以看出数组索引是 (n-1) & hash 这里其实是位运算得出数组索引
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;   //判断key完全相同则返回内容
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;
}
``````

#### 索引冲突

``````hash = hash(key); //通过key获取hash值
n = tab.length; //获取数组长度 值一定是2的n次方(可以看HashMap中resize方法了解细节)
index = (n - 1) & hash  //在存值和取值的时候，算出index，数组的索引
``````

table.length 是一个 2的n次方值，也就是说 n-1 一定是 0000011111这种类似结构

n = 8； 二进制 1000

n – 1 = 7 二进制 0111

(n-1) & 1010110 = 0000111 & 1010110 它的结果是 0110 换算十进制为 6，直接找到元素位置table[6];

hashmap在实际使用时可能有key不同，索引值相同的情况。

table[1] = node1;

node1.key = key1,node1.value = value1 node1.next = node2

node2.key = key2,node2.value = value2 node2.next = null;

#### 链表改为树结构

``````if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st  链表改为树的逻辑判断
treeifyBin(tab, hash);

// TREEIFY_THRESHOLD的定义
static final int TREEIFY_THRESHOLD = 8;
``````