TreeMap overview

TreeMap class implements Map interface and its built using tree based data structure

Main features of TreeMap


TreeMap is similar to any other Map which stores key-value pair

Elements in TreeMap will be in sorted order based on keys sorting

It contains only unique keys

It does not allow NULL key but NULL values are allowed

TreeMap can be created using constructor as below

  1. TreeMap tm = new TreeMap();
TreeMap tm = new TreeMap();


Important methods in TreeMap


put(Object key, Object value)

This method stores the specified value and associates it with the specified key in this map.

get(Object key)

This method will return the value associated with a specified key in this map.

firstEntry()

Returns a key-value mapping associated with the least key in this map, or null if the map is empty.

firstKey()

Returns the first (lowest) key currently in this map.

lastKey()

Returns the last (highest) key currently in this map

size()

Returns the number of key-value mappings in this map.

Example

  1. import java.util.*;
  2. public class TreeMapExample {
  3. public static void main(String args[]) {
  4.  
  5. TreeMap<Integer,String> tm = new TreeMap<>();
  6.  
  7. tm.put(5, "Five");
  8. tm.put(3, "Three");
  9. tm.put(2, "Two");
  10. tm.put(4, "Four");
  11. tm.put(1, "One");
  12. System.out.println(TreeMap is …”);
  13.  
  14. System.out.println(tm);
  15. System.out.println("Value for the key 1 is "+tm.get(1));
  16. System.out.println("Value for the key 10 is "+tm.get(10));
  17.  
  18. System.out.println("First key in the map is "+tm.firstKey());
  19.  
  20. System.out.println("Last key in the map is "+tm.lastKey());
  21.  
  22. System.out.println("Number of elements in map is "+tm.size());
  23.  
  24. }
  25. }
import java.util.*;
public class TreeMapExample {
public static void main(String args[]) {

TreeMap<Integer,String> tm = new TreeMap<>();

tm.put(5, "Five");
tm.put(3, "Three");
tm.put(2, "Two");
tm.put(4, "Four");
tm.put(1, "One");
System.out.println(“TreeMap is …”);

System.out.println(tm);
System.out.println("Value for the key 1 is "+tm.get(1));
System.out.println("Value for the key 10 is "+tm.get(10));

System.out.println("First key in the map is "+tm.firstKey());

System.out.println("Last key in the map is "+tm.lastKey());

System.out.println("Number of elements in map is "+tm.size());

}
}


When to use TreeMap ?

Whenever we want to store data in key-value pair format and whenever we need to have elements in the sorted order

When not to use TreeMap ?

Whenever we don’t want to store key-value pair data and Whenever we don’t want to store elements in sorted order

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on