java - hashmap的负载因子和容量

标签 java

如何找到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/

相关文章:

java - Oracle解析 "' {S}Paris 2"as a string literal then finds {S} which it is treating as a SQL Escape Sequence which it doesn'的文本并识别

java - 如何Parcelable查看对象的成员?

java - 散列和取消散列?

java - 从 JMenuBar 控制 JFrame

java - MongoDB 的reduce-phase 没有按预期工作

java - 在 Google App Engine Flex 上运行时使用 Java 将文件从 Google Storage Bucket 移动到 Google Drive

java - Java Triple DES 加密/解密的 PHP 等价物

java - 如何制作金色分形树

java - 为什么我无法将 SuperType 添加到 <T extends SuperType> 的 List<T> 中?

java - 如何限制 akka actor 一次只做一项工作