Let’s learn TreeMap in java.
TreeMap in java
- Underlying data structure for TreeMap is Red-Black Tree.
- Insertion order is not preserved in TreeMap.
- In TreeMap all elements will be inserted according to some sorting order of Keys.
- Duplicate keys are not allowed. But duplicate values are allowed.
- For empty TreeMap, first entry with null key is allowed. But after inserting that entry if we insert any other null entry then we will get run time exception saying NullPointerException.
Let’s see TreeMap example.
import java.util.TreeMap; class TreeMapDemo { public static void main(String[] args) { TreeMap m = new TreeMap(); m.put(101, "Ravi"); m.put(103, "Yash"); m.put(102, "Tejas"); m.put(103, 106); System.out.println(m); } }
Output:
{101=Ravi, 102=Tejas, 103=106}
In the above java program if we add heterogeneous keys then ClassCastException is thrown.
import java.util.TreeMap; class TreeMapDemo { public static void main(String[] args) { TreeMap m = new TreeMap(); m.put(101, "Ravi"); m.put(103, "Yash"); m.put(102, "Tejas"); m.put(103, 106); m.put("Abhi", "Tejas"); System.out.println(m); } }
Output:
Exception in thread “main” java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.String
Now in the above non-empty TreeMap if we insert “null” key then NullPointerException is thrown. Because if we compare anything with “null” NullPointerException is thrown.
import java.util.TreeMap; class TreeMapDemo { public static void main(String[] args) { TreeMap m = new TreeMap(); m.put(101, "Ravi"); m.put(103, "Yash"); m.put(102, "Tejas"); m.put(103, 106); m.put(null, "Tejas"); System.out.println(m); } }
Output:
Exception in thread “main” java.lang.NullPointerException
Methods of treemap
Method | Description |
K firstKey() | returns the first (lowest) key currently in this map. |
V get(Object key) | returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. |
Object clone() | returns a shallow copy of this TreeMap instance. |
void clear() | removes all of the mappings from this map. |
boolean containsKey(Object key) | returns true if this map contains a mapping for the specified key. |
boolean containsValue(Object value) | returns true if this map maps one or more keys to the specified value. |
NavigableSet<K> descendingKeySet() | Returns a reverse order NavigableSet view of the keys contained in this map. |
NavigableMap<K,V> descendingMap() | returns a reverse order view of the mappings contained in this map. |
Set<Map.Entry<K,V>> entrySet() | returns a Set view of the mappings contained in this map. |
Map.Entry<K,V> firstEntry() | returns a key-value mapping associated with the least key in this map, or null if the map is empty. |
Map.Entry<K,V> floorEntry(K key) | returns a key-value mapping associated with the greatest key less than or equal to the given key, or null if there is no such key. |
K floorKey(K key) | returns the greatest key less than or equal to the given key, or null if there is no such key. |
SortedMap<K,V> headMap(K toKey) | returns a view of the portion of this map whose keys are strictly less than toKey. |
NavigableMap<K,V> headMap(K toKey, boolean inclusive) | returns a view of the portion of this map whose keys are less than (or equal to, if inclusive is true) toKey. |
K higherKey(K key) | returns the least key strictly greater than the given key, or null if there is no such key. |
Set<K> keySet() | returns a Set view of the keys contained in this map. |
Map.Entry<K,V> lastEntry() | returns a key-value mapping associated with the greatest key in this map, or null if the map is empty. |
K lastKey() | returns the last (highest) key currently in this map. |
Map.Entry<K,V> lowerEntry(K key) | returns a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such key. |
K lowerKey(K key) | returns the greatest key strictly less than the given key, or null if there is no such key. |
NavigableSet<K> navigableKeySet() | returns a NavigableSet view of the keys contained in this map. |
Map.Entry<K,V> pollFirstEntry() | removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty. |
Map.Entry<K,V> pollLastEntry() | removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty. |
V put(K key, V value) | Associates the specified value with the specified key in this map. |
void putAll(Map<? extends K,? extends V> map) | copies all of the mappings from the specified map to this map. |
V remove(Object key) | removes the mapping for this key from this TreeMap if present. |
int size() | returns the number of key-value mappings in this map. |
NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) | returns a view of the portion of this map whose keys range from fromKey to toKey. |
SortedMap<K,V> subMap(K fromKey, K toKey) | returns a view of the portion of this map whose keys range from fromKey, inclusive, to toKey, exclusive. |
SortedMap<K,V> tailMap(K fromKey) | returns a view of the portion of this map whose keys are greater than or equal to fromKey. |
NavigableMap<K,V> tailMap(K fromKey, boolean inclusive) | returns a view of the portion of this map whose keys are greater than (or equal to, if inclusive is true) fromKey. |
K ceilingKey(K key) | returns the least key greater than or equal to the given key, or null if there is no such key. |
Treemap constructors
TreeMap t = new TreeMap(); for default natural sorting order.
TreeMap t = new TreeMap(Comparator c); for customized sorting order.
TreeMap t = new TreeMap(SortedMap m);
TreeMap t = new TreeMap(Map m);
Reference – Oracle Docs