As I know in java8 HashMap bucket implementation was changed a bit. If bucket size exceeds some value then list transforms to the "balanced tree".
I don't understand
1. what type of balanced tree used in Oracle JDK? (AVL? red-black? something like in databases for indexes?)
2. Is it binary tree?
3. As I understand sorting executes according hashcode. For example in my bucket I have 102 elements. 100 values with hashcode equally 12 (I understand that it worth but I just need to understand this behaviour) and 2 with hashcode 22.
How does search will be executed for value ?
HashMap stores elements in so-called buckets and the number of buckets is called capacity. When we put a value in the map, the key's hashCode() method is used to determine the bucket in which the value will be stored. To retrieve the value, HashMap calculates the bucket in the same way – using hashCode().
I was going through java 8 features and found out that hashmaps use a red black tree instead of a linkedlist when the number of entry sets on the bucket increases.
HashMap contains an array of the nodes, and the node is represented as a class. It uses an array and LinkedList data structure internally for storing Key and Value.
HashMap allows a single null key and multiple null values. TreeMap does not allow null keys but can have multiple null values. HashMap allows heterogeneous elements because it does not perform sorting on keys. TreeMap allows homogeneous values as a key because of sorting.
Looking at the implementation, it looks like a binary tree. More specifically, the comment below suggests it's a red-black tree :
static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {
TreeNode<K,V> parent; // red-black tree links
TreeNode<K,V> left;
TreeNode<K,V> right;
TreeNode<K,V> prev; // needed to unlink next upon deletion
boolean red;
...
}
As for handling equal hash codes, this is addressed in the Javadoc :
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.
The TreeNode
s used in HashMap
are said to be structured similarly to those of TreeMap
.
You can see the implementation of the search for a TreeNode
containing the required key here :
/**
* Finds the node starting at root p with the given hash and key.
* The kc argument caches comparableClassFor(key) upon first use
* comparing keys.
*/
final TreeNode<K,V> find(int h, Object k, Class<?> kc) {
TreeNode<K,V> p = this;
do {
int ph, dir; K pk;
TreeNode<K,V> pl = p.left, pr = p.right, q;
if ((ph = p.hash) > h)
p = pl;
else if (ph < h)
p = pr;
else if ((pk = p.key) == k || (k != null && k.equals(pk)))
return p;
else if (pl == null)
p = pr;
else if (pr == null)
p = pl;
else if ((kc != null ||
(kc = comparableClassFor(k)) != null) &&
(dir = compareComparables(kc, k, pk)) != 0)
p = (dir < 0) ? pl : pr;
else if ((q = pr.find(h, k, kc)) != null)
return q;
else
p = pl;
} while (p != null);
return null;
}
As you can see, this is similar to the standard binary search tree search. First they search for a TreeNode
having the same hashCode
as the searched key (since a single bucket of a HashMap
may contain entries having different hashCode
s). Then it proceeds until a TreeNode
having a key equal to the required key is found. The secondary search uses compareTo
if the class of the key implements Comparable
. Otherwise, a more exhaustive search is performed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With