针对少量条目最快地实现Java Map [英] Quickest implementation of Java Map for a small number of entries

查看:112
本文介绍了针对少量条目最快地实现Java Map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.util的最快实施方式是什么?映射以获得极少数条目(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.

推荐答案

如果所有条目都可以表示为枚举,请使用 EnumMap

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


此实现将Map
接口的丰富性和安全性与接近数组的速度相结合。如果你想
将枚举映射到一个值,你应该总是使用优先级为
的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.

如果不是, HashMap 是好的解决方案它为基本操作提供恒定时间,例如 get() put()

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


这个实现为基本的
操作(get和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.

只需在你的HashMap中设置低容量值: / p>

Just rembember to set low capacity value in your HashMap:


因此,如果迭代性能是非常重要的话,不要将初始容量设置得太高(或者b $ b加载因子太低)很重要。

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

当然,上面的实现不是线程安全的。在这种情况下,最好的线程安全实现将是 ConcurrentHashMap的。它结合了HashMap的高性能和线程安全性。

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.

这是不同Map实现的良好比较。

Here is good comparison of different Map implementations.

编辑:这是一个有趣的比较不同的 HashMap 实现。看来,至少对于原始类型,有比内置 HashMap 更快的替代方案。

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:由于Peter Lawrey的回答,我决定进行一些测试,比较 ConcurrentHashMap Collections.synchronizedMap()

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);
    }

}

我的结果是:

===== ConcurrentHashMap =====

===== ConcurrentHashMap =====


  • 5000000看跌期权(关键,价值) - 0.99714195 s

  • 5000000 puts(key, value) - 0.99714195 s

5000000 get(key) - 0.452227427 s

5000000 get(key) - 0.452227427 s

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

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


  • 5000000看跌期权(关键,价值) - 0.586431367 s

  • 5000000 puts(key, value) - 0.586431367 s

5000000 get(key) - 0.376051088 s

5000000 get(key) - 0.376051088 s

所以,Peter可能是对的 - 对于小地图, Collections.synchronizedMap()更快。

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

这篇关于针对少量条目最快地实现Java Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆