TreeMap Class – Java Collections

Today i am going to write about TreeMap in details. I receive many requests from hackpundit user to write a blog on this topic  in details, as there is not much content on the internet. In this blog I will reference examples which will help you understand TreeMap class in more details.

TreeMap Class

Assumptions

  • Eclipse IDE is installed in your system and you have basic knowledge of how to use Eclipse IDE as all the examples will be done on Eclipse IDE .
  • You have basic knowledge of Java like Interface, Abstract class.
  • You have must have good knowledge of Oops concept.

So lets get started with Java TreeMap class:

TreeMap - Map Hierarchy

Map hierarchy

In the above image you can see the Java Map Hierarchy.

  • Here Map, Sorted Map, and Navigable Map are the Interface, that means it has only abstract methods.
  • HashTable, LinkedHashMap, HashMap and TreeMap are the Concrete Class.
  • An Concrete Class in Java is the derived class, which provide basic implementation of all the methods declared in interface or Abstract class.
  • So here Tree map is a Concrete class which implements Navigable Map(Interface).
  • Navigable Map(Interface) extends Sorted Map and Sorted Map(Interface) implements Map(Interface).
  • A Tree Map works on the basis of Key-Value pair.
  • A Tree Map provides efficient means of sorting on the basis of key in ascending order.
  • A Tree Map contain only unique value. It does not take NULL value as key, but can have NULL values.

Below i have explain the constructor of TreeMap class:

  • TreeMap(): 

    It is used to create a empty Tree Map that will sorts according to natural orders of its key.

For Example:

 

  • TreeMap(comparator <? super K> comparator>): 

    This constructor constructs an empty tree map that will be sort the Map according to user needs or  using the Comparator comp.

For Example:

  • TreeMap(SortedMap < K ,? extends V > m):

    This constructor Constructs a new tree map containing the same mappings and using the same ordering as the specified sorted map.

 

  • TreeMap(Map<? extends K,? extends V> m):

    This constructor Constructs a new tree map containing the same mappings as the given map, ordered according to the natural ordering of its keys.

    • All keys inserted into the new map must implement the Comparable Interface.
    •  This method runs in n*log(n) time.

Now i will explain the following methods which are defined in TreeMap:

CeilingEntry(K key): This method provides result according to key. It provide value greater than or equal to key. If the key and greater key is not present then it give NULL values(Example 4: Line 21-23).

 

Clear(): The clear() method is used to remove all of the mappings or values from this map. The map will get empty after calling this method(Example 4: Line 114-119).

 

Clone(): This method is used to return a similar copy of TreeMap instance(Example 4: Line 123-138).

 

Comparator(): The comparator() method is used to order map as per user needs, or otherwise it will give NULL values if the map uses the natural ordering of its keys.

 

ContainsKey(Object key): This method checks weather the specific KEY is present is the map or not. It returns ‘True’ if the key is present else ‘False’.(Example 4: Line 25-28).

 

ContainsValue(Object value): This method checks weather the specific VALUE is present is the map or not. It returns ‘True’ if the key is present else ‘False’.(Example 4: Line 30-32)

 

DescendingKeySet(): This method is used to return a reverse order or in descending order of the keys contained in this map.(Example 4: Line 34-37)

 

DescendingMap():  This method is used to return a reverse order or descending order contained in this map.(Example 4: Line 39-42)

 

EntrySet(): This method is used to return a Set view of the mappings contained in this map. This method  returns the entries in ascending key order.(Example 4: Line 44-47)

 

firstEntry(): This  method is used to return the first key-value after sorting the values in Map. If the Map is empty then it will return Null.(Example 4: Line 49-50)

 

firstKey(): This method will return the first or lowest Key after sorting the values in map.(Example 4: Line 52-53)

 

get(Object key): This method is used to return the value to which the specified key is mapped, or Null if this map contains no value mapped for the key.(Example 4: Line 55-57)

 

headMap(K toKey): This  method is used to return all the key-values in the map which are less than the specified key.(Example 4: Line 59-65)

 

higherEntry(K key): This method is used to return a next higher key-value mapping according to the specified key, and it will return Null if there is no such key.(Example 4: Line67-69)

 

lastEntry(): This  method will return the greatest or last  key-value mapping . If the Map is empty then it will return Null.(Example 4: Line 71-72)

 

navigableKeySet(): This method is used to return a NavigableSet of all the keys in the map. The set return the keys in Ascending order.(Example 4: Line 74-75)

 

pollFirstEntry(): This method is used to remove and returns the firstkey-value in the Map. If the Map is empty then it will return Null.(Example 4: Line 77-80)

 

pollLastEntry(): This method is used to remove and returns the last key-value in the map. It returns Null if the map is empty.(Example 4: Line 82-85)

 

put(K key,V value): This method is used to Add key-Value in the map. If the map previously have some value for specific Key, ans you are adding value for the same key then the old value will be replaced.(Example 4: Line 15-19)

 

putAll(Map<? extends K,? extends V> map): This method is used to copy all the key value from 1st map to 2nd map. These mappings replace any mappings that this map had for any of the keys currently in the specified map.(Example 4: Line 87-97)

 

remove(Object key): This method is used to remove the value for the key from the TreeMap if present. If the key is not present it will return Null value.(Example 4: Line 99-105)

 

replace(K key, V value): This method is used to replace the value according to key.(Example 4: Line 107-109)

 

size(): This method is used to return the number of key-value mappings in the map.(Example 4: Line 111-112)

 

I have implemented all the methods in the below example for references:

 

In this blog I haven’t explain all the methods, but explained all the complicated and important methods. If I have missed any point or you have any doubt regarding this blog just let me know in the comment section. If you like my article please like our Facebook page and also follow us on Twitter so that you get regular updates. For regular updates you can also subscribe to hackpundit.com with your email.

Please also share on Facebook and Twitter to help other developers. Thank You..!