RFR (XXS) 8080535: (ch) Expected size of Character.UnicodeBlock.map is not optimal

Martin Buchholz martinrb at google.com
Wed May 20 19:57:21 UTC 2015

I'd like to contribute (the start of) a test that checks that static
HashMaps are not oversized.  I suggest using that consistently for static
HashMaps (and ArrayLists?) throughout the JDK:

import java.lang.reflect.*;
import java.util.*;

@SuppressWarnings({"unchecked", "rawtypes"})
public class StaticMapSize {

    static int tableLength(HashMap map) throws Throwable {
        Field tableField = HashMap.class.getDeclaredField("table");
        return ((Object[]) tableField.get(map)).length;

    public static void main(String[] args) throws Throwable {
        Class<?>[] klazzes =
        for (Class<?> klazz : klazzes) {
            for (Field field : klazz.getDeclaredFields()) {
                if (Modifier.isStatic(field.getModifiers())) {
                    Object x = field.get(null);
                    if (x instanceof HashMap) {
                        HashMap map = (HashMap) x;
                        HashMap copy = new HashMap(map);
                        if (tableLength(map) != tableLength(copy)) {
                            String msg = String.format
                                ("map %s has excess capacity: need %d; use
                                 field.toString(), tableLength(copy),
                            throw new AssertionError(msg);

More information about the core-libs-dev mailing list