Quickest implementation of Java Map for a small number of entries

前端 未结 3 1584
遇见更好的自我
遇见更好的自我 2021-02-13 13:15

What is the quickest implementation of java.util.Map for a very small number of entries (under 15 elements or so)? Both thread-safe and non-thread-safe.

3条回答
  •  失恋的感觉
    2021-02-13 13:20

    If all entries can be represented as Enums, use EnumMap:

    This implementation combines the richness and safety of the Map interface with a speed approaching that of an array. If you want to map an enum to a value, you should always use an EnumMap in preference to an array.

    If no, HashMap is good solution. It provides constant time for basic operations, like get() and put():

    This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets.

    Just rembember to set low capacity value in your HashMap:

    Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important.

    Of course, above implementations are not thread safe. The best thread-safe implementation in this case will be ConcurrentHashMap. It combines high performance of HashMap with being thread-safe.

    Here is good comparison of different Map implementations.

    Edit: Here is an interesting comparison of different HashMap implementations. It seems, that, at least for primitive types, there are faster alternatives than built-in HashMap.

    Edit2: Due to Peter Lawrey's answer I decided to perform some test, comparing ConcurrentHashMap and Collections.synchronizedMap() :

    public static void main(String[] args) {
          System.out.println("\n===== ConcurrentHashMap =====");
          testMap(new ConcurrentHashMap<>());
          System.out.println("\n===== Collections.synchronizedMap()  =====");
          testMap(Collections.synchronizedMap(new HashMap<>()));
    }
    
        static final int N = 5;
        static final int R = 1000000;
    
        public static void testMap(Map map) {
            long startTime = System.nanoTime();
    
            System.out.println("\n-- " + N*R + " puts(key, value) --");
            startTime = System.nanoTime();
            for (int j = 0; j < R; j++) {
                for (int i = 0; i < N; i++) 
                    map.put(i, new float[] { 0f, 1f, 2f, 3f, 4f });
                map.clear();
            }
            System.out.println((System.nanoTime() - startTime) / 1000000000.0);
    
            System.out.println("\n-- " + N*R + " get(key) --");
            startTime = System.nanoTime();
            for (int j = 0; j < R; j++) {
                for (int i = 0; i < N; i++)  
                    map.get(i); 
                map.clear();
            }
            System.out.println((System.nanoTime() - startTime) / 1000000000.0);
        }
    
    }
    

    My results are:

    ===== ConcurrentHashMap =====

    • 5000000 puts(key, value) - 0.99714195 s

    • 5000000 get(key) - 0.452227427 s

    ===== Collections.synchronizedMap() =====

    • 5000000 puts(key, value) - 0.586431367 s

    • 5000000 get(key) - 0.376051088 s

    So, Peter is probably right - for small maps, Collections.synchronizedMap() is faster.

提交回复
热议问题