Common methods of Java TreeMap

TreeMap is used to store HashMapkey-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 Setview 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 Setview 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);

[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-0ptbKiN6-1601948160181)(image-20201005233217235.png)]

  • 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);

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-0kHbwYGA-1601948030869)(image-20201005233417838.png)]

  • 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);

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-Ce9b84Dd-1601948030872)(image-20201005233559835.png)]

  • 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);

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-vVz6xpgJ-1601948030876)(image-20201005233708430.png)]

  • 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);

[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-OPIYTszR-1601948030878)(image-20201005233803739.png)]

  • 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);

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-GHrJjdee-1601948030879)(image-20201005233852340.png)]

  • 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);

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-EGsDTBB8-1601948030881)(image-20201005234055871.png)]

  • 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);

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-rhj6afe5-1601948030882)(image-20201005234219702.png)]

  • 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);

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-EUlFlfnR-1601948030884)(image-20201006092006102.png)]

  • 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);

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-CFjjiKLl-1601948030886)(image-20201006092110649.png)]

  • 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);

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-8qCRXEej-1601948030887)(image-20201006092219924.png)]

  • 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);

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-v0puAx2R-1601948030889)(image-20201006092326285.png)]

  • 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);

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-siEDnVKD-1601948030890)(image-20201006092603426.png)]

  • 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);

[External link image transfer failed. The source site may have an anti-leeching mechanism. It is recommended to save the image and upload it directly (img-AgfOlLHS-1601948030892)(image-20201006092652724.png)]

  • 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);

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-1xYAPmia-1601948030894)(image-20201006092756027.png)]

  • 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);

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-oH2M5oM3-1601948030895)(image-20201006092940671.png)]

  • 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);

[External link image transfer failed. The source site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-vpLg7Vsi-1601948030897)(image-20201006093255011.png)]

  • 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

Guess you like

Origin blog.csdn.net/qq_43229056/article/details/108936356