在HashMap中,为什么阈值(调整大小的下一个大小值)是capacity*load factor.为什么不等于地图的大小或容量.
例如,最初默认容量= 16,负载因子= 0.75因此threshold = (capacity * load factor) = (16 * 0.75) = 12.
添加第13个元素的地图调整大小为什么这样,为什么地图的作者决定保留它capacity * load factor(12个)?为什么不与容量相同(16).
为什么不保持阈值等于容量,以便只在hashmap满了时才进行重组?
如果您知道,请说明原因.我用Google搜索,但没有找到解释清楚的答案.
当你hashCode是负面时,是否使桶的指数为正?
我有以下代码
int a = 01111;
System.out.println("output1 = " + a);
System.out.println("output2 = " + Integer.toOctalString(1111));
Run Code Online (Sandbox Code Playgroud)
和输出是
Run Code Online (Sandbox Code Playgroud)output1 = 585 output2 = 2127
我期待输出如下所示。
Run Code Online (Sandbox Code Playgroud)output1 = 2127 output2 = 2127
为什么585当我打印直接 int 值时它会给出?我期待java自动将前导零的值转换为八进制。
01111和之间是什么关系585?
这是我的测试课..
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
A a = new A(0, 1);
HashMap<A, Integer> map = new HashMap<A, Integer>();
map.put(a, (a.x + a.y));
System.out.println(map.containsKey(a));
System.out.println("----------------- ");
System.out.println(map.containsKey(new A(0, 1)));
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的A类,其中包含hashcode和eclipse生成的相等方法.
class A {
int x, y;
public A(int x, int y) {
super();
this.x = x;
this.y = y;
}
@Override
public int hashCode() {
System.out.println(" in hashcode");
final int prime = 31;
int result = 1;
result = prime * …Run Code Online (Sandbox Code Playgroud)