java·数据结构·hashMap
2020-12-13 03:10
标签:执行 int htable stat 根据 为什么 变量 float 比较 java·数据结构·hashMap 标签:执行 int htable stat 根据 为什么 变量 float 比较 原文地址:https://www.cnblogs.com/kunlingou/p/11067857.html特点
Collections.synchronizedMap(new HashMap
方式创建构造方法
相关参数
平衡与折衷
代码解析
public HashMap(int initialCapacity, float loadFactor) {
/**初始最大容量为非负整数*/
if (initialCapacity MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
/**
* 加载因子为大于0的浮点数
* public static boolean isNaN(float v) {
* return (v != v);
* }
* Float.isNaN(loadFactor):NaN(not-a-number),例如. float v = 0.0f/0.0f;
*/
if (loadFactor 32,65->128
*/
static final int tableSizeFor(int cap) {
int n = cap - 1;
n |= n >>> 1;
n |= n >>> 2;
n |= n >>> 4;
n |= n >>> 8;
n |= n >>> 16;
return (n = MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
}
public HashMap(int initialCapacity) {
this(initialCapacity, DEFAULT_LOAD_FACTOR);
}
/**
* 在resize()方法中设置threshold的值
* newCap = DEFAULT_INITIAL_CAPACITY;
* newThr = (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);
*/
public HashMap() {
this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted
}
public HashMap(Map extends K, ? extends V> m) {
this.loadFactor = DEFAULT_LOAD_FACTOR;
putMapEntries(m, false);
}
小结: