TreeMap is used to store
HashMap
key-value pairs that are very similar to classes. The difference is that TreeMap provides an efficient way to store key/value pairs in sorted order . It is based on the realization of red-black treesNavigableMap
.
public class TreeMap<K,V>
extends AbstractMap<K,V>
implements NavigableMap<K,V>, Cloneable, java.io.Serializable
One, commonly used methods
-
void clear(): It deletes all key-value pairs from the map.
-
int size(): Returns the number of key-value pairs present in this map.
-
boolean containsKey(Object key): If this map contains a map with the specified key, it will be returned
true
. -
boolean containsValue(Object value): If this map maps one or more keys to the specified value, return it
true
. -
Set<Map.Entry<K,V>> entrySet( ): Returns the
Set
view of the map contained in this map . Often used for traversal. -
V get(Object key): Return to the value mapped by the specified key, if not, return
null
. -
Set KeySet(): Returns a
Set
view of the keys contained in this map . Often used to traverse the Key in the map. -
V put(K key, V value): Associate the specified value with the specified key in this map. That is, the key-value pair is added to the map.
-
V remove(Object key): Delete the key-value pair of the specified key.
-
V replace(K key, V value): Replace the value of the specified key.
-
V replace(K key,V oldValue,V newValue): Replace the value of the specified key-value pair. If the specified key-value pair does not exist, no operation is performed.
Two, unique methods
- Map.Entry <K,V> ceilingEntry(K key): Returns the smallest element greater than or equal to the specified Key, if not, returns null
TreeMap treeMap = new TreeMap();
treeMap.put("1", "demo1");
treeMap.put("2", "demo2");
treeMap.put("3", "demo3");
Entry entry = treeMap.ceilingEntry("2.5");
System.out.println(entry+"====="+entry.getKey()+"->"+entry.getValue());
entry = treeMap.ceilingEntry("4");
System.out.println(entry);
- K ceilingKey(K key): Returns the smallest Key greater than or equal to the specified Key, if not, returns null
TreeMap treeMap = new TreeMap();
treeMap.put("1", "demo1");
treeMap.put("2", "demo2");
treeMap.put("3", "demo3");
Object obj = treeMap.ceilingKey("2.5");
System.out.println(obj);
obj = treeMap.ceilingEntry("4");
System.out.println(obj);
- NavigableSetdescendingKeySet(): Returns all the keys of the set, in reverse order
TreeMap treeMap = new TreeMap();
treeMap.put("1", "demo1");
treeMap.put("2", "demo2");
treeMap.put("3", "demo3");
Set set = treeMap.descendingKeySet();
System.out.println(set);
- NavigableMap<K,V> descendingMap(): returns the collection, and is in reverse order
TreeMap treeMap = new TreeMap();
treeMap.put("1", "demo1");
treeMap.put("2", "demo2");
treeMap.put("3", "demo3");
System.out.println(treeMap);
Map map = treeMap.descendingMap();
System.out.println(map);
- Map.Entry<K,V> firstEntry(): Returns the element with the smallest Key in the collection
TreeMap treeMap = new TreeMap();
treeMap.put("1", "demo1");
treeMap.put("2", "demo2");
treeMap.put("3", "demo3");
Entry entry = treeMap.firstEntry();
System.out.println(entry);
- K firstKey(): Returns the smallest Key in the set
TreeMap treeMap = new TreeMap();
treeMap.put("1", "demo1");
treeMap.put("2", "demo2");
treeMap.put("3", "demo3");
Object obj = treeMap.firstKey();
System.out.println(obj);
- Map.Entry<K,V> floorEntry(K key): Contrary to the ceilingEntry() method, it returns the largest element that is less than or equal to the specified Key
TreeMap treeMap = new TreeMap();
treeMap.put("1", "demo1");
treeMap.put("2", "demo2");
treeMap.put("3", "demo3");
Entry entry = treeMap.floorEntry("2.5");
System.out.println(entry);
- K floorKey(K key): Returns the largest Key less than or equal to the specified Key
TreeMap treeMap = new TreeMap();
treeMap.put("1", "demo1");
treeMap.put("2", "demo2");
treeMap.put("3", "demo3");
Object obj = treeMap.floorKey("2.5");
System.out.println(obj);
- SortedMap<K,V> headMap(K Key): Returns all elements less than the specified Key
TreeMap treeMap = new TreeMap();
treeMap.put("1", "demo1");
treeMap.put("2", "demo2");
treeMap.put("3", "demo3");
Map map = treeMap.headMap("2.5");
System.out.println(map);
- NavigableMap<K,V> headMap(K Key,boolean inclusive): When inclusive is true, return all elements whose Key is less than or equal to the specified Key
TreeMap treeMap = new TreeMap();
treeMap.put("1", "demo1");
treeMap.put("2", "demo2");
treeMap.put("3", "demo3");
Map map = treeMap.headMap("2",true);
System.out.println(map);
map = treeMap.headMap("2",false);
System.out.println(map);
- Map.Entry<K,V> higherEntry(K key): Returns all elements greater than the specified Key
TreeMap treeMap = new TreeMap();
treeMap.put("1", "demo1");
treeMap.put("2", "demo2");
treeMap.put("3", "demo3");
Entry entry = treeMap.higherEntry("2.5");
System.out.println(entry);
- K higherKey(K Key): Return all keys greater than the specified Key
TreeMap treeMap = new TreeMap();
treeMap.put("1", "demo1");
treeMap.put("2", "demo2");
treeMap.put("3", "demo3");
Object obj = treeMap.higherKey("2.5");
System.out.println(obj);
- Map.Entry<K,V> lastEntry(): Returns the element with the largest Key
TreeMap treeMap = new TreeMap();
treeMap.put("1", "demo1");
treeMap.put("2", "demo2");
treeMap.put("3", "demo3");
Entry entry = treeMap.lastEntry();
System.out.println(entry);
- K lastKey(): Returns the largest Key
TreeMap treeMap = new TreeMap();
treeMap.put("1", "demo1");
treeMap.put("2", "demo2");
treeMap.put("3", "demo3");
Object obj = treeMap.lastKey();
System.out.println(obj);
- Map.Entry<K,V> lowerEntry(K Key): Returns the largest element less than the specified key
TreeMap treeMap = new TreeMap();
treeMap.put("1", "demo1");
treeMap.put("2", "demo2");
treeMap.put("3", "demo3");
Entry entry = treeMap.lowerEntry("2.5");
System.out.println(entry);
-
K lowerKey(K Key): Returns the largest Key less than the specified Key
-
Map.Entry<K,V> pollFirstEntry(): delete the element with the smallest Key
TreeMap treeMap = new TreeMap();
treeMap.put("1", "demo1");
treeMap.put("2", "demo2");
treeMap.put("3", "demo3");
treeMap.pollFirstEntry();
System.out.println(treeMap);
-
Map.Entry<K,V> pollLastEntry(): Delete the element with the largest Key
-
NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive): intercept the elements of the key from fromKey to toKey in the collection, whether to intercept them, depends on true or false
TreeMap treeMap = new TreeMap();
treeMap.put("1", "demo1");
treeMap.put("2", "demo2");
treeMap.put("3", "demo3");
treeMap.put("4", "demo4");
Map map = treeMap.subMap("1",true, "4", true);
System.out.println(map);
map = treeMap.subMap("1",false, "4", true);
System.out.println(map);
map = treeMap.subMap("1",false, "4", false);
System.out.println(map);
-
SortedMap<K,V> subMap(K fromKey, K toKey): intercept the elements of Key from fromKey to toKey in the collection, including fromKey but not toKey
-
SortedMap<K,V> tailMap(K fromKey): intercept all elements whose Key is greater than or equal to fromKey
-
NavigableMap<K,V> tailMap(K fromKey, boolean fromInclusive): When inclusive is true, intercept all elements whose Key is greater than or equal to fromKey, otherwise intercept all elements whose Key is greater than fromKey