如何找到hashmap当前的负载因子和容量?
Map m = new HashMap(10,1);
//but after lots of works
Here how to get current values.
最佳答案
您不应该能够获得负载系数和容量;它们是 hashmap 类的实现细节。但是,您可以使用反射。但尽量避免使用它,这通常是一个坏主意。
Field f1 = m.getClass().getDeclaredField("table");
f1.setAccessible(true);
int capacity = f1.get(m).length;
Field f2 = m.getClass().getDeclaredField("threshold");
f2.setAccessible(true);
int currentLoadFactor = f2.get(m);
关于java - hashmap的负载因子和容量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19968861/