In this chapter you will learn:
- What is TreeMap class in Java?
- TreeMap Programming Example
- Constructors and Methods of TreeMap class in Java
What is TreeMap class in Java?
TreeMap class in Java uses Tree based Map to store elements. A Tree based map provides faster searching and retrieval of data and sorting is based on natural order of keys(More often it is an ascending order). A TreeMap is not implicitly Synchronized, it means multiple thread can access TreeMap but only one Thread have right to modify treemap. However, you can explicitly sync treemap class in java. More about TreeMap class in Java is listed below:
- A TreeMap class stores values based on Key.
- It contains only unique elements.
- It doesn’t accept null key however it can have multiple null values.
- It is almost same as HashMap but it maintains ascending order.
Constructors and Methods
Constructors of Java TreeMap class
Constructor | Description |
---|---|
TreeMap() | It is used to construct an empty tree map that will be sorted using the natural order of its key |
TreeMap(Comparator comp) | It is used to construct an empty tree-based map that will be sorted using the comparator comp. |
TreeMap(Map m) | It is used to initialize a tree map with the entries from m, which will be sorted using the natural order of the keys. |
TreeMap(SortedMap sm) | It is used to initialize a tree map with the entries from the SortedMap sm, which will be sorted in the same order as sm. |
Methods | Description |
---|---|
boolean containsKey(Object key) | It is used to return true if this map contains a mapping for the specified key. |
boolean containsValue(Object value) | It is used to return true if this map maps one or more keys to the specified value. |
Object firstKey() | It is used to return the first (lowest) key currently in this sorted map. |
Object get(Object key) | It is used to return the value to which this map maps the specified key. |
Object lastKey() | It is used to return the last (highest) key currently in this sorted map. |
Object remove(Object key) | It is used to remove the mapping for this key from this TreeMap if present. |
void putAll(Map map) | It is used to copy all of the mappings from the specified map to this map. |
Set entrySet() | It is used to return a set view of the mappings contained in this map. |
int size() | It is used to return the number of key-value mappings in this map. |
Collection values() | It is used to return a collection view of the values contained in this map. |
Programming Example
import java.util.*; public class TreeMap_Example { public static void main(String[] args) { TreeMap<Integer,String> tm=new TreeMap<Integer,String>(); tm.put(1,"Jack"); tm.put(3,"Clark"); tm.put(2,"Steven"); tm.put(4,"Sarah"); //Direct Printing System.out.println(tm); //Iteration Method 1 PrintMethod1(tm); //Iteration Method 2 PrintMethod2(tm); System.out.println("Item Removed : "+tm.remove(2)); System.out.println("First Key of this list is : "+tm.firstKey()); System.out.println("Last Key of this list is : "+tm.lastKey()); System.out.println("Value of Key 3 is : "+tm.get(3)); } //Iteration Method 1 public static void PrintMethod1(TreeMap<Integer,String> tm) { Set set=tm.entrySet(); Iterator itr=set.iterator(); while(itr.hasNext()) { Map.Entry me=(Map.Entry)itr.next(); System.out.println("Key is : "+me.getKey()+" and Value is : "+me.getValue()); } } //Iteration Method 2 public static void PrintMethod2(TreeMap<Integer,String> tm) { for(Map.Entry m:tm.entrySet()) { System.out.println("Key : "+m.getKey() + " | Value = "+m.getValue()); } } }
Output
Key is : 1 and Value is : Jack
Key is : 2 and Value is : Steven
Key is : 3 and Value is : Clark
Key is : 4 and Value is : Sarah
Key : 1 | Value = Jack
Key : 2 | Value = Steven
Key : 3 | Value = Clark
Key : 4 | Value = Sarah
Item Removed : Steven
First Key of this list is : 1
Last Key of this list is : 4
Value of Key 3 is : Clark
_
Summary
In this chapter you learned TreeMap class in Java with complete and easy programming example. My aim is to teach you Java with the help of easy programming not the help of bulky theory. Once you will be familiar with collection objects you will start exploring collection more deeply than tutorial.