Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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:
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.
The storing order maintained by the treemap must be consistent with equals just like any other sorted map, irrespective of the explicit comparators. The treemap implementation is not synchronized in the sense that if a map is accessed by multiple threads, concurrently and at least one of the threads modifies the map structurally, it must be synchronized externally.
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.
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.
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.
Course Name | Date | Details |
---|---|---|
Java Course Online | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co