1

What is the time and space complexity of the new implement of hashmap in java 8 (with the tree improvement) and is it concurrent?

Is there an O(n) complexity, for example when the tree in the bucket is not balanced?

Where can I find this implement?

Thank you.

  • 1
    Why don't you have a look at the [Java documentation](http://docs.oracle.com/javase/8/docs/api/index.html?java/util/HashMap.html). Most of your questions are answered there. Do you want to now the best, average or worst case of time complexity? – Flown May 21 '16 at 15:14
  • I have read it, but did not found any information on it's worst case – user6365005 May 21 '16 at 15:20
  • 3
    Possible duplicate of [HashMap get/put complexity](http://stackoverflow.com/questions/4553624/hashmap-get-put-complexity) – Jonathan May 21 '16 at 15:21
  • no, it's not, I'm talking about java 8 version – user6365005 May 21 '16 at 15:30
  • 1
    The concurrency question can be answered by the documentation that @Flown has already linked. – the8472 May 21 '16 at 15:50

1 Answers1

4

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.

Note: This is an implementation detail, thus subject to change and does not apply to all Java implementations, e.g. it might be different on android. The comment is from 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 (i.e., 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.

Also note that Big-O notations generally omit another factor that depends on the complexity of the compare operation, e.g. if they are string comparisons they also depend on the common prefix length of the strings.

the8472
  • 35,110
  • 4
  • 54
  • 107
  • So it acts just like a regular hash map in its complexity. Is there an O(n) case, as I have mentioned? – user6365005 May 21 '16 at 15:39
  • 1
    Your first conclusion is inaccurate at best. As for the question: Yes, that can be concluded from the quoted comment. – the8472 May 21 '16 at 15:45
  • changing the implement from binary tree to avl tree will improve the complexity (especially in this O(n) case) ? – user6365005 May 21 '16 at 15:53
  • 1
    That sounds like a separate question to me and one that's more about general computer-science (data structures and algorithmic complexity theory) than specific to java. – the8472 May 21 '16 at 15:54
  • Ok, I will ask in other forum here. By the way, where have you found the new implement? – user6365005 May 21 '16 at 16:17
  • @user6365005: your question makes no sense. An AVL tree *is* a binary tree, and `HashMap` is using exactly that when saying “binary tree” and it has `O(log n)` time complexity in that case. The `O(n)` worst case is referring to cases where using a binary tree (be it AVL or not) is impossible, i.e. for unequal objects having the same hash code and not being comparable. In that rare situation, there is nothing you can do to avoid the `O(n)` time complexity. – Holger May 23 '16 at 10:47