The TreeMap class creates maps stored in a tree structure. It stores the key/ value pairs in a sorted order efficiently. The TreeMap class extends AbstractMap and implements the NavigableMap interface. It is a red-black tree based implementation. TreeMap is a sorted order collection, which is sorted according to the natural key or by providing comparator. Its element will be sorted in ascending key order. It can be customized using comparable and comparator interfaces.
TreeMap Class Declaration
public class TreeMap<K,V> extends AbstractMap<K,V> implements NavigableMap<K,V> , cloneable, serializable
Here parameter K indicates the type of keys and V indicates the type of values.
TreeMap Class Constructors
Constructor | Description |
---|---|
TreeMap( ) | It constructs an empty tree map. |
TreeMap(Compartor comparator) | It constructs empty tree map which is sorted by using comparator. |
TreeMap(Map m) | It initializes the tree map with the entries of given map which is sorted in the same order as the given map. |
TreeMap(SortedMap m) | It initializes the tree map with the entries of given map, which is sorted by using natural order of the keys. |
TreeMap Class Example
package javabeat.net.corejava; import java.util.TreeMap; public class SimpleTreeMap { public static void main(String[] args) { TreeMap< Integer, String> employees = new TreeMap<Integer, String>(); employees.put(3,"Ganesh"); employees.put(1,"Vikrant"); employees.put(4,"Maruti"); employees.put(2,"Mahantesh"); System.out.println("Original map: "+ employees); } }
- The above simple example demonstrates usage of TreeMap class.
- TreeMap employees = new TreeMap(); line creates an TreeMap which accepts the string and integer value and creates TreeMap object using new operator.
- employees.put(3,”Ganesh”); line adds the specified key and string value. Similarly, the other elements are also added in the list.
- System.out.println(“Original map: “+ employes); line displays the list of map in the output.
TreeMap Class Methods
Methods | Description |
---|---|
Map.Entry ceilingEntry(K key) | It returns the least key which is greater than or equal to the given key for key value mapping. |
K ceilingKey(K key) | According to specified key, this method returns least key that is greater than or equal to in the map. |
Object clone( ) | Creates a duplicate copy of the specified instance of TreeMap. |
void clear( ) | It removes all key values from the tree map. |
NavigableSet descendingKeySet( ) | The key set containing in the map returns in a reverse order. |
NavigableMap descendingMap( ) | The key containing in the map returns in a reverse order view of the mapping. |
NavigableMap headMap(K toKey boolean inclusive) | It returns the map view portion, where the keys are less than the toKey. |
NavigableSetnavigableKeySet( ) | It views a NavigableSet of keys that are present in this map. |
NavigableMapsubMap(K fromKey, boolean fromInclusive, KtoKey, boolean toInclusive) | This method returns the map view portion from the range fromKey to toKey. |
NavigableMaptailMap(K fromKey, boolean inclusive) | It determines the map view portion, where keys are greater than the fromKey. |
boolean containsKey(Object key) | If a map contains mapping for the specified key then it returns true, else it returns false. |
boolean containsValue(Object value) | If one or more keys are map with the specified key, then it returns as true. |
Set<Map.Entry>entrySet( ) | This map contains set of mapping. |
Set keySet( ) | It contains set of keys. |
Map.EntryfirstEntry( ) | It returns first entry in the map with the least key value. |
Map.EntryfloorEntry(K key) | It returns the largest key in the map. |
Map.EntryhigherEntry(K key) | It returns the greatest key than the specified key. |
Map.EntrylastEntry( ) | It returns last entry in the map with the greatest key. |
Map.EntrylowerEntry(K key) | It returns the set of greatest key than the specified key. |
Map.Entry pollFirstEntry(K key) | It specifies the first entry removing the entry associated with least key. |
Map.Entry pollLastEntry(K key) | It returns the last entry removing the entry associated with the greatest key. |
K firstKey( ) | It returns the first key from the map. |
K floorKey( ) | It returns key which is less than or equal to the specified key. |
K higherKey( ) | It returns a key which is greater than the specified key. |
K lastKey( ) | It returns the last key from the map. |
K lowerKey( ) | It returns the key which is strictly less than the specified key. |
V get(Object key) | It specifies the value which the key is specified in mapping. |
V put(K key, V value) | With the specified key, a specified value is been associated in the map. |
V remove( Object key) | If any key is present in the TreeMap, it removes the key from the mapping. |
SortedMapheadMap(K toKey) | It returns the map view portion, where keys are strictly less than toKey. |
SortedMaptailMap(K toKey) | It specifies the map view portion, where keys are greater than the fromKey. |
SortedMapsubMap(K fromKey, K toKey) | It returns the map view portion, where the key range is fromKey(inclusive) to toKey(exclusive). |
Collection values( ) | It returns a collection containing the values in the map. |
int size( ) | It returns the number of size of the map. |
Comparator comparator( ) | It returns the order of keys using comparator in the map. |
void putAll(Mapmap) | All the mappings are been copied from the specified map. |
TreeMap Methods Example
package javabeat.net.corejava; import java.util.TreeMap; import java.util.Comparator; public class TreeMap1 { public static void main(String[] args) { TreeMap< Integer, String> employes = new TreeMap<Integer, String>(); employes.put(3,"Ganesh"); employes.put(1,"Vikrant"); employes.put(4,"Maruti"); employes.put(2,"Mahantesh"); Comparator comp = employes.comparator(); System.out.println("Size : "+employes.size()); System.out.println("Comparator value: "+ comp); System.out.println("Value for 3rd place exists: "+ employes.containsKey(3)); System.out.println("1st place name is: "+ employes.firstEntry()); System.out.println("At 3rd place the name is : "+ employes.get(3)); System.out.println("Value number of last place is: "+ employes.floorKey(4)); System.out.println("Removed 3rd value: "+employes.remove(3)); System.out.println("After modification the list is: "+ employes); } }
- In the above program we have used TreeMap class. We have used different types of methods in this example i.e. size(),comparator(),containsKey( ), firstEntry(),get( ), floorKey( ) and remove( ).
- TreeMap employes = new TreeMap(); line creates an TreeMap which accepts the string and integer value and creates TreeMap object using new operator.
- employes.put(3,”Ganesh”); line add the specified key and string value. Similarly, the other elements are also added in the list.
- Comparator comp = employes.comparator(); line returns the order of keys using comparator in the map.
- System.out.println(“Size : “+employes.size()); line specifies the size of the map.
- System.out.println(“Value for 3rd place exists: “+ employes.containsKey(3)); statement is to print the specified key value of the mapping that is present in the list.
- System.out.println(“1st place name is: “+ employes.firstEntry()); statement indicates the first entry in the map with the least key.
- System.out.println(“At 3rd place the name is : “+ employes.get(3)); statement returns the value of the specified key in mapping.
- System.out.println(“Value number of last place is: “+ employes.floorKey(4)); line specifies last value that is present in the list.
- System.out.println(“Removed 3rd value: “+employes.remove(3)); line removes a specified value from the list.