What is a time complexity for sets and maps created with a factory methods Set.of() and Map.of()?

微笑、不失礼 提交于 2020-01-03 00:54:07

问题


In Java, when I create a set with Set.of() or a map with Map.of() what is the time complexity of the contains and get operations? Is it O(1)?


回答1:


The Set.of and Map.of APIs return instances of JDK-private implementations. The performance of these implementations is not guaranteed by the specification. However, the APIs do return specific implementations about which performance statements can be made. Thus, the question is reasonable and is distinct from a (hypothetical) question such as "What is the performance of Map.get?" which is a poor question because there are many different Map implementations.

In any case, the implementations behind Set.of (for size greater than two) and Map.of (for size greater than one) use a simple open addressed hashing scheme with linear probing for collision resolution. The Set.contains and Map.get operations are O(1) if the elements' (keys') hashes are reasonably well distributed.



来源:https://stackoverflow.com/questions/57557125/what-is-a-time-complexity-for-sets-and-maps-created-with-a-factory-methods-set-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!