ConcurrentHashMap.newKeySet() vs Collections.newSetFromMap()

前端 未结 2 775
走了就别回头了
走了就别回头了 2021-02-04 05:18

Java 8 introduced new way to obtain a concurrent Set implementation

// Pre-Java-8 way to create a concurrent set
Set oldStyle = Collec         


        
2条回答
  •  灰色年华
    2021-02-04 05:54

    ConcurrentHashMap.newKeySet() should be somewhat more efficient as removes a single level of indirection. Collections.newSetFromMap(map) is mostly based on redirecting the operations to the map.keySet(), but ConcurrentHashMap.newKeySet() is very close to map.keySet() itself (just with additions support).

    As for functionality, I see no difference.

提交回复
热议问题