简体   繁体   English

Java 8中的hashmap的复杂性是什么?

[英]What is the complexity of hashmap in java 8?

What is the time and space complexity of the new implement of hashmap in java 8 (with the tree improvement) and is it concurrent? Java 8中新的hashmap实现的时间和空间复杂度是什么(随着树的改进),并且它是并发的吗?

Is there an O(n) complexity, for example when the tree in the bucket is not balanced? 是否存在O(n)复杂性(例如,当存储桶中的树不平衡时)?

Where can I find this implement? 我在哪里可以找到这个工具?

Thank you. 谢谢。

If you actually look at the source - which is bundled with the JDK and should be automatically opened by the IDE if you look at the class, you will see the comment quoted below. 如果您实际查看的是源代码-它与JDK捆绑在一起,并且如果您查看该类,则应该由IDE自动打开它,您将看到下面引用的注释。

Note : This is an implementation detail , thus subject to change and does not apply to all Java implementations, eg it might be different on android. 注意 :这是一个实现细节 ,因此可能会发生变化,并且不适用于所有Java实现,例如,在android上可能有所不同。 The comment is from OpenJDK 1.8 注释来自OpenJDK 1.8

  /* [...] * This map usually acts as a binned (bucketed) hash table, but * when bins get too large, they are transformed into bins of * TreeNodes, each structured similarly to those in * java.util.TreeMap. Most methods try to use normal bins, but * relay to TreeNode methods when applicable (simply by checking * instanceof a node). Bins of TreeNodes may be traversed and * used like any others, but additionally support faster lookup * when overpopulated. However, since the vast majority of bins in * normal use are not overpopulated, checking for existence of * tree bins may be delayed in the course of table methods. * * Tree bins (ie, bins whose elements are all TreeNodes) are * ordered primarily by hashCode, but in the case of ties, if two * elements are of the same "class C implements Comparable<C>", * type then their compareTo method is used for ordering. (We * conservatively check generic types via reflection to validate * this -- see method comparableClassFor). The added complexity * of tree bins is worthwhile in providing worst-case O(log n) * operations when keys either have distinct hashes or are * orderable, Thus, performance degrades gracefully under * accidental or malicious usages in which hashCode() methods * return values that are poorly distributed, as well as those in * which many keys share a hashCode, so long as they are also * Comparable. (If neither of these apply, we may waste about a * factor of two in time and space compared to taking no * precautions. But the only known cases stem from poor user * programming practices that are already so slow that this makes * little difference.) [...] */ 

Note the O(log n) tree bin behavior being conditional on properties of the inserted keys. 请注意,O(log n)树箱的行为取决于插入键的属性。

Also note that Big-O notations generally omit another factor that depends on the complexity of the compare operation, eg if they are string comparisons they also depend on the common prefix length of the strings. 还应注意,Big-O表示法通常会省略另一个取决于比较操作复杂性的因素,例如,如果它们是字符串比较,则它们还取决于字符串的公共前缀长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM