TREEMAP implements the SortedMap and Navigablemap interfaces, all of which are recorded in SortedMap and navigablemap reading notes.
SortedMap
1. The comparison of the sort should be consistent with equals (Object)
2. Four types of "standard" constructors should be provided
1). No parameter constructor
2). A constructor with a Comparator parameter
3). A constructor with a Map parameter
4). A constructor with a sortedmap parameter
3. subMap , headmap , tailmap , KeySet , values, entryset The map or Set returned by the method and the sortedmap itself use the same data, so modifying the map returned by SUBMAP will also be reflected on the sortedmap.
Navigablemap
1. lowerentry, floorentry, ceilingentry, higherentry , respectively, return less than, less than or equal to, greater than or equal to, and greater than the given Key's map.entry. This type of method is used to locate the element closest to the given value of the target.
2. The operation of the growth sequence map is faster than the descending order map.
3. The method returned by entry returns the entry snapshot for that moment, so the Entry.setvalue method is usually not supported.
For example, TreeMap implements the Navigablemap Firstentry, which returns the immutable value of the new variable based on the given entry key.
The Simpleimmutableentry object.
4. pollfirstentry Delete and return the first element
TreeMap
1. Implementation based on red and black trees
2. According to the natural order, or the given comparator is the inner element remains orderly.
3. ContainsKey, get, put, remove operation with log (n) complexity
4. Itertator adopts fast-fail mechanism
5. Values are inherited from Collection, and EntrySet and KeySet are inherited from Set
6. deletionentry Delete the specified element, fixafterdeletion the deleted tree node to rebalance, so that TreeMap maintain the characteristics of the red black tree.
7. Containsvalue (Object) determines whether to include the specified value as value by traversing all elements. Therefore, the efficiency is low.
public boolean Containsvalue (Object value) {for (entry<k,v> e = Getfirstentry (); e = null; e = successor (e))
if (Valequals (value, E.value)) return true; return false; }
8. getfirstentry Returns the element in the bottom-left corner of the tree
Final entry<k,v> Getfirstentry () { entry<k,v> p = root; if (P! = null) while (p.left! = null) p = p.left; return p; }
getlastentry Returns the element in the lower-right corner of the tree
Final entry<k,v> Getlastentry () { entry<k,v> p = root; if (P! = null) while (p.right! = null) p = p.right; return p; }
9. successor (Entry e)
When e is null, returns null
Returns the leftmost descendant node of the right child node when E has a right child node
When e does not have a right child node, it returns an ancestor node that is closest to E, and the left child of the ancestor stanza is also the ancestor of E.
Static <K,V> treemap.entry<k,v> successor (Entry<k,v> t) { if (t = = null) return null; else if (t.right! = null) { entry<k,v> p = t.right; while (p.left! = null) p = p.left; return p; } else { entry<k,v> p = t.parent; entry<k,v> ch = t; while (P! = NULL && ch = = p.right) { ch = p; p = p.parent; } return p; } }
prodecessor (Entry e) and successor (Entry e) have similar ideas.
When e is null, returns null
When E has a left dial hand node, it returns the lower-right descendant node of the left child node
When e has no left child node, it returns an ancestor node closest to E, and the right child of the ancestor stanza is also the ancestor of E.
Static <K,V> entry<k,v> predecessor (Entry<k,v> t) { if (t = = null) return null; else if (t.left! = null) { entry<k,v> p = t.left; while (p.right! = null) p = p.right; return p; } else { entry<k,v> p = t.parent; entry<k,v> ch = t; while (P! = NULL && ch = = p.left) { ch = p; p = p.parent; } return p; } }
One. get (object): v and getentry (object): The difference between Entry is that the former returns V and the latter returns Entry. Get the same algorithm, because get is implemented based on Getentry.
Public V get (Object key) { entry<k,v> p = getentry (key); Return (P==null null:p.value); }
ContainsKey (Object) is also implemented based on Getentry.
public boolean ContainsKey (Object key) { return getentry (key)! = NULL; }
computeredlevel (int) , which is used to copy a map to an operation that is currently empty TreeMap. The copied TreeMap should be a complete binary tree, which, by applying black to the nodes that satisfy the perfect binary tree (perfect binary trees), can simply implement the black attribute of the red and black tree.
private static int computeredlevel (int sz) { int level = 0; for (int m = sz-1; M >= 0; m = m/2-1) level++; return level; }
Here is an example of a complete binary tree, which satisfies the perfect binary tree with only 0-7 nodes, or 0-2 layers. The 0-2-layer nodes are all black, and the last layer is all painted red. It is most convenient to meet the characteristics of red and black trees.
buildfromsorted, using the idea of recursion, first constructs the left subtree of the rear node, constructs the right subtree of the node, and finally combines the nodes into a complete subtree.
Putall (MAP),
When the TREEMAP has no elements, the map is a sortmap, and the map comparator equals the TreeMap comparator, buildformsorted is used to build the TREEMAP.
Otherwise, each mapping in the MAP is inserted into the TREEMAP by calling put (K, V).
public void Putall (map<? extends K,? extends v> Map) {int mapsize = Map.size (); if (size==0 && mapsize!=0 && map instanceof sortedmap) {comparator<?> c = ((sortedmap< ,?,? >) map. Comparator (); if (c = = Comparator | | (c! = null && c.equals (comparator))) {++modcount; try {buildfromsorted (mapsize, Map.entryset (). iterator (), NULL, NULL ); } catch (Java.io.IOException Cannothappen) {} catch (ClassNotFoundException Cannothappen) { } return; }} super.putall (map); }
getentry, getentryusingcomparator, getceilingentry, getfloorentry, Gethigherentry, getlowerentry are based on binary search ideas to implement element operations.
. put (K, V)
The value of this Entry is updated directly when there is already an equal Entry of key and K.
Otherwise, insert the new Entry, and then self-balance the tree structure.
Remove (Object), delete the specified node, and then self-balance the tree structure
Clear (), and then root to null
public void Clear () { modcount++; size = 0; root = null; }
firstentry Returns an immutable Entry, getfirstentry returns a variable Entry. The same relationship:lastentry and getlastentry,lowerentry and getlowerentry, higherentry and gethigherentry.
JDK version: JDK1.8.0_31.JDK
[Java] TREEMAP-Source Code Learning notes