Implementing a Map interface in Java is a very important task. For this purpose, we have TreeMap and HashMap. In this article our focus will be on TreeMap in Java in the Following Order:
- What is a TreeMap in Java?
- Features of TreeMaps
- Constructors in TreeMap
- Methods in TreeMap
- Example of TreeMap in Java
What is a TreeMap in Java?
A TreeMap in Java is used to implement Map interface and NavigableMap along with the Abstract Class. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used. This proves to be an efficient way of sorting and storing the key-value pairs.
Features of TreeMaps
This class is a member of the Java Collections Framework.
The class implements Map interfaces including NavigableMap, SortedMap and extends AbstractMap
TreeMap in Java does not allow null keys (like Map) and thus a NullPointerException is thrown. However, multiple null values can be associated with different keys.
All Map.Entry pairs returned by methods in this class and its views represent snapshots of mappings at the time they were produced.
They do not support the Entry.setValue method.
Important Points to Remember
Apart from implementing the Map interface, Java TreeMap also implements NavigableMap and indirectly implements SortedMap interface. TreeMap also extends AbstractMap class.
TreeMap entries are sorted in the natural ordering of its keys. It also provides a constructor to provide Comparator to be used for ordering. So if you are using any class as key, make sure it’s implementing Comparable interface for natural ordering. Check out java collections interview questions to understand the importance of these methods.
Java TreeMap implementation provides guaranteed log(n) time cost for the containsKey, get, put and remove operations.
TreeMap is not synchronized and hence not thread-safe. For multithreaded environments, you can get a wrapped synchronized using Collections.synchronizedSortedMap method.
TreeMap methods to get keyset and values return Iterator that is fail-fast in nature, so any concurrent modification will throw ConcurrentModificationException.
TreeMap in java doesn’t allow null keys, however, you can have multiple null values associated with different keys.
Constructors in TreeMap
Methods in TreeMap
Example of TreeMap in Java
import java.util.TreeMap; public class TreeMapMain { public static void main(String args[]) { // TreeMap with Country as key and capital as value // TreeMap stores elements in natural ordering of keys. TreeMap<String,String> countryCapitalMap=new TreeMap<String,String>(); countryCapitalMap.put("India","Delhi"); countryCapitalMap.put("Japan","Tokyo"); countryCapitalMap.put("France","Paris"); countryCapitalMap.put("Russia","Moscow"); System.out.println("-----------------------------"); // Iterating TreeMap Using keySet() and for each loop System.out.println("Iterating TreeMap Using keySet() and for each loop"); for (String countryKey:countryCapitalMap.keySet()) { System.out.println("Country:"+ countryKey +" and Capital:"+countryCapitalMap.get(countryKey)); } System.out.println("-----------------------------"); } }
Output:
With this, we come to an end of this TreeMap in Java article. Check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this “TreeMap in Java” blog and we will get back to you as soon as possible.