< prev index next >
test/java/lang/Character/UnicodeBlock/NonOptimalMapSize.java
Print this page
rev 12008 : [mq]: XXXXXXX-Remove-INITIAL_CAPACITY-constant
@@ -40,18 +40,22 @@
Map<?,?> map = (Map<?,?>)mapField.get(null);
if (!map.getClass().equals(HashMap.class)) {
throw new RuntimeException(
"Character.UnicodeBlock.map is expected to be HashMap");
}
- int mapSize = map.size();
- Field sizeField = ubCls.getDeclaredField("INITIAL_CAPACITY");
- sizeField.setAccessible(true);
- int INITIAL_CAPACITY = sizeField.getInt(null);
+ // This is the initial size of Character.UnicodeBlock.map
+ // See src/java.base/share/classes/java/lang/Character.java
+ int initialCapacity = (int)(510 / 0.75f + 1.0f);
+
+ // We will check that the capacity was sufficient:
+ // - create a new HashMap with this initial capacity
+ // - insert map.size() elements in it
+ // - check that the internal storage wasn't reallocated
// Construct a HashMap with specified initial capacity
- HashMap<Object,Object> map1 = new HashMap<>(INITIAL_CAPACITY);
+ HashMap<Object,Object> map1 = new HashMap<>(initialCapacity);
Class<?> hmCls = HashMap.class;
Field tableField = hmCls.getDeclaredField("table");
tableField.setAccessible(true);
// ... and fill it up
map1.put(new Object(), new Object());
< prev index next >