// 1.计算 hash code 并获取 index
final int hash;
int index;
if (key == null) {
// 为空直接取 0
hash = 0;
index = indexOfNull();
} else {
// 否则取 Object.hashCode()
hash = mIdentityHashCode ? System.identityHashCode(key) : key.hashCode();
index = indexOf(key, hash);
}
// 2.如果 index 大于等于 0 ,说明之前存在相同的 hash code 且 key 也相同,则直接覆盖
if (index >= 0) {
index = (index<<1) + 1;
final V old = (V)mArray[index];
mArray[index] = value;
return old;
}
// 3.如果没有找到则上面的 indexOf() 或者 indexOfNull() 就会返回一个负数,而这个负数就是由将要插入的位置 index 取反得到的,所以这里再次取反就变成了将进行插入的位置
index = ~index;
// 4.判断是否需要扩容
if (osize >= mHashes.length) {
final int n = osize >= (BASE_SIZE2) ? (osize+(osize>>1))
Android开源项目《ali1024.coding.net/public/P7/Android/git》
: (osize >= BASE_SIZE ? (BASE_SIZE2) : BASE_SIZE);
if (DEBUG) Log.d(TAG, "put: grow from " + mHashes.length + " to " + n);
final int[] ohashes = mHashes;
final Object[] oarray = mArray;
// 5.申请新的空间
allocArrays(n);
if (CONCURRENT_MODIFICATION_EXCEPTIONS && osize != mSize) {
throw new ConcurrentModificationException();
}
if (mHashes.length > 0) {
if (DEBUG) Log.d(TAG, “put: copy 0-” + osize + " to 0");
// 将数据复制到新的数组中
System.arraycopy(ohashes, 0, mHashes, 0, ohashes.length);
System.arraycopy(oarray, 0, mArray, 0, oarray.length);
}
// 6.释放旧的数组
freeArrays(ohashes, oarray, osize);
}
if (index < osize) {
// 7.如果 index 在当前 size 之内,则需要将 index 开始的数据移到 index + 1 处,以腾出 index 的位置
if (DEBUG) Log.d(TAG, "put: move " + index + “-” + (osize-index)
- " to " + (index+1));
System.arraycopy(mHashes, index, mHashes, index + 1, osize - index);
System.arraycopy(mArray, index << 1, mArray, (index + 1) << 1, (mSize - index) << 1);
}
if (CONCURRENT_MODIFICATION_EXCEPTIONS) {
if (osize != mSize || index >= mHashes.length) {
throw new ConcurrentModificationException();
}
}
// 8.然后根据计算得到的 index 分别插入 hash,key,以及 code
mHashes[index] = hash;
mArray[index<<1] = key;
mArray[(index<<1)+1] = value;
mSize++;
return null;
}
put 方法调用了其他几个内部的方法,其中关于扩容以及如何释放空间,申请新的空间这些,从算法层来讲其实不重要,只要知道一点就是,扩容会发生数据的复制,这个是会影响效率的就可以了。
而与算法相关性较大的 indexOfNull() 方法以及 indexOf() 方法的实现。由于这两个方法的实现基本一样,因此这里只分析 indexOf() 的实现。
int indexOf(Object key, int hash) {
final int N = mSize;
// Important fast case: if nothing is in here, nothing to look for.
if (N == 0) {
return ~0;
}
int index = binarySearchHashes(mHashes, N, hash);
// If the hash code wasn’t found, then we have no entry for this key.
if (index < 0) {
return index;
}
// If the key at the returned index matches, that’s what we want.
if (key.equals(mArray[index<<1])) {
return index;
}
// Search for a matching key after the index.
int end;
for (end = index + 1; end < N && mHashes[end] == hash; end++) {
if (key.equals(mArray[end << 1])) return end;
}
// Search for a matching key before the index.
for (int i = index - 1; i >= 0 && mHashes[i] == hash; i–) {
if (key.equals(mArray[i << 1])) return i;
}
// Key not found – return negative value indicating where a
// new entry for this key should go. We use the end of the
// hash chain to reduce the number of array entries that will
// need to be copied when inserting.
return ~end;
}
其实它原来的注释已经很详细了,详细的步骤是:
-
(1) 如果当前为空表,则直接返回 ~0,注意不是 0 ,而是最大的负数。
-
(2) 在 mHashs 数组中进行二分查找,找到 hash 的 index。
-
(3) 如果 index < 0,说明没有找到。
-
(4) 如果 index >= 0,且在 mArray 中对应的 index<<1 处的 key 与要找的 key 又相同,则认为是同一个 key,说明找到了。
-
(5) 如果 key 不相同,说明只是 hash code 相同,那么分别向后和向前进行搜索,如果找到了就返回。如果没找到,那么对 end 取反就是当前需要插入的 index 位置。
再回过头来看 put() 方法, put() 方法的具体实现都在源码中加以了详细的说明,感兴趣的可以详细阅读一下。而从 put 方法得出以下几个结论:
-
(1) mHashs 数组以升序的方式保存了所有的 hash code。
-
(2) 通过 hash code 在 mHashs 数组里的 index 值来确定 key 以及 value 在 mArrays 数组中的存储位置。一般来说分别就是 index << 1 以及 index << 1 + 1。再简单点说就是 index * 2 以及 index * 2 + 1。
-
(3) hashCode 必然可能存在冲突,这里是怎么解决的呢?这个是由上面的第 3 步和第 7 步所决定。第 3 步是得出应该插入的 index 的位置,而第 7 步则是如果 index < osize ,则说明原来 mArrays 中必然已经存在相同 hashCode 的值了,那么就把数据全部往后移一位,从而在 mHashs 中插入多个相同的 hash code 并且一定是连接在一起的,而在 mArrays 中插入新的 key 和 value,最终得以解决 hash 冲突。
上面的结论可能还是让人觉得有点晕,那么再来看看下面的图吧,就一定能明白了。
上面图说, index == 0 时 和 index == 1时的 hash code 是一样的,说明 key1 与 key2 的 hash code 是一样的,也就是存在 hash 冲突了。那么,如上,这里的解决办法就是 hash code 存储了 2 份,而 key-value 分别存储一份。
- get() 方法
public V get(Object key) {
final int index = indexOfKey(key);
return index >= 0 ? (V)mArray[(index<<1)+1] : null;
}
主要就是通过 indexOfKey() 计算出 index,而 indexOfKey() 的实现就是调用 indexOfNull () 和 indexOf(),其具体的实现已经上面分析过了。这里如果返了 index >= 0,则说明一定是找到了,那么根据前面的规则,在 mArray 中,index<<1 + 1 就是所要获取的 value 了。
- remove() 方法
public V remove(Object key) {
final int index = indexOfKey(key);
if (index >= 0) {
return removeAt(index);
}
return null;
}
首先通过 indexOfKey() 计算出 index 以判断其是否存在,如果存在则进一步调用 removeAt() 来删除相应的 hash code 以及 key-value。
public V removeAt(int index) {
final Object old = mArray[(index << 1) + 1];
final int osize = mSize;
final 《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》开源 int nsize;
// 如果 size 小于等于1 ,移除后数组长度将为 0。为了压缩内存,这里直接将mHashs 以及 mArray 置为了空数组
if (osize <= 1) {
// Now empty.
if (DEBUG) Log.d(TAG, “remove: shrink from " + mHashes.length + " to 0”);
final int[] ohashes = mHashes;
最后我想说
为什么很多程序员做不了架构师?
1、良好健康的职业规划很重要,但大多数人都忽略了
2、学习的习惯很重要,持之以恒才是正解。
3、编程思维没能提升一个台阶,局限在了编码,业务,没考虑过选型、扩展
4、身边没有好的架构师引导、培养。所处的圈子对程序员的成长影响巨大。
金九银十面试季,跳槽季,整理面试题已经成了我多年的习惯!在这里我和身边一些朋友特意整理了一份快速进阶为Android高级工程师的系统且全面的学习资料。涵盖了Android初级——Android高级架构师进阶必备的一些学习技能。
附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…
导、培养。所处的圈子对程序员的成长影响巨大。
金九银十面试季,跳槽季,整理面试题已经成了我多年的习惯!在这里我和身边一些朋友特意整理了一份快速进阶为Android高级工程师的系统且全面的学习资料。涵盖了Android初级——Android高级架构师进阶必备的一些学习技能。
附上:我们之前因为秋招收集的二十套一二线互联网公司Android面试真题(含BAT、小米、华为、美团、滴滴)和我自己整理Android复习笔记(包含Android基础知识点、Android扩展知识点、Android源码解析、设计模式汇总、Gradle知识点、常见算法题汇总。)
[外链图片转存中…(img-T9se1sXL-1650444794989)]
里面包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中…