TreeMap in Java with Example
Java TreeMap implements the NavigableMap interface. It extends Map Interface. Java TreeMap is based on the red-black Tree implementation. It stores the key-value pair in sorted order. The values based on the key. It contains unique elements, i.e. it does not contain the duplicate values. It does not provide null keys but contains multiple null values. It maintains the ascending order. If you assign a null key, then it throws NullPointerException. It is not synchronized, so it is not thread-safe.
Time Complexity of TreeMap:
TreeMap based on Red-Black Tree data structure. The complexity of the TreeMap is O(log n) time.
The constructor of TreeMap:
- TreeMap (): It is used to construct the empty TreeMap which is natural sorted.
- TreeMap (Map m): It is used to initialize the treemap with the entries of map m which is natural sorted.
- TreeMap (Comparator c): It is used to construct the empty tree-based map which sorted by using Comparator.
- TreeMap (SortedMap s): It is used to initializes the treemap with entries from s, which sorted as SortedMaps.
Methods of TreeMap:
- containsKey (Object key): it returns true if the specified key is present in the TreeMap.
Example:
import java.util.*;
public class TreeMapDemo {
public static void main (String [] args) {
TreeMap<Integer, String> l = new TreeMap<Integer, String> ();
l.put (10, "Java");
l.put (15, "HTML");
l.put (20, "Python");
l.put (25, "JavaScript");
l.put (30, "C++");
System.out.println ("Initial Mappings are: " + l);
System.out.println ("Is the key '10' present?" + l.containsKey (10));
System.out.println ("Is the key '55' present?" + l.containsKey (55));
} }
Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
Is the key '10' present? true
Is the key '55' present? false
- containsValue (Object value): It returns true if the specified value is present in the TreeMap.
Example:
import java.util.*;
public class TreeMapDemo {
public static void main (String [] args) {
TreeMap<Integer, String> l = new TreeMap<Integer, String> ();
l.put (10, "Java");
l.put (15, "HTML");
l.put (20, "Python");
l.put (25, "JavaScript");
l.put (30, "C++");
System.out.println ("Initial Mappings are: " + l);
System.out.println ("Is the value 'Java' present?" + l.containsValue ("Java"));
System.out.println ("Is the value 'Python' present?" + l.containsValue ("Python"));
} }
Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
Is the value 'Java' present? true
Is the value 'Python' present? true
- get (Object Key): It is used to get the value of a particular key from the map.
Example:
import java.util.*;
public class TreeMapDemo {
public static void main (String [] args) {
TreeMap<Integer, String> l = new TreeMap<Integer, String> ();
l.put (10, "Java");
l.put (15, "HTML");
l.put (20, "Python");
l.put (25, "JavaScript");
l.put (30, "C++");
System.out.println ("Initial Mappings are: " + l);
System.out.println ("The value is: " + l.get (20));
System.out.println ("The value is: " + l.get (25));
} }
Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
The value is: Python
The value is: JavaScript
- put (object K, object V): It is used to insert the value of the specified key in the Map.
Example:
import java.util.*;
public class TreeMapDemo {
public static void main (String [] args) {
TreeMap<Integer, String> l = new TreeMap<Integer, String> ();
l.put (10, "Java");
l.put (15, "HTML");
l.put (20, "Python");
l.put (25, "JavaScript");
l.put (30, "C++");
System.out.println ("Initial Mappings are: " + l);
String return= (String) l.put (20, "All");
System.out.println ("Returned value is: " + return);
System.out.println ("New map is: " + l);
} }
Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
Returned value is: Python
New map is: {10=Java, 15=HTML, 20=All, 25=JavaScript, 30=C++}
- clear (): It is used to remove all mapping from the TreeMap.
Example:
import java.util.*;
public class TreeMapDemo {
public static void main (String [] args) {
TreeMap<Integer, String> l = new TreeMap<Integer, String>();
l.put (10, "Java");
l.put (15, "HTML");
l.put (20, "Python");
l.put (25, "JavaScript");
l.put (30, "C++");
System.out.println ("Initial Mappings are: " + l);
l.clear ();
System.out.println ("After clearing the map: "+ l);
} }
Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
After clearing the map: {}
- clone (): This is used to return the same copy of the TreeMap.
Example:
import java.util.*;
public class TreeMapDemo {
public static void main (String [] args) {
TreeMap<Integer, String> l = new TreeMap<Integer, String> ();
l.put (10, "Java");
l.put (15, "HTML");
l.put (20, "Python");
l.put (25, "JavaScript");
l.put (30, "C++");
System.out.println ("Initial Mappings are: " + l);
System.out.println ("The cloned map look like this: " + l.clone ());
} }
Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
The cloned map look like this: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
- size (): This method is used to return the size of the TreeMap.
Example:
import java.util.*;
public class TreeMapDemo {
public static void main (String [] args) {
TreeMap<Integer, String> l = new TreeMap<Integer, String>();
l.put (10, "Java");
l.put (15, "HTML");
l.put (20, "Python");
l.put (25, "JavaScript");
l.put (30, "C++");
System.out.println ("Initial Mappings are: " + l);
System.out.println ("The map size is: " + l.size ());
} }
Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
The map size is: 5
- putAll (Map m): This is used to copy all elements from the map to another map.
Example:
import java.util.*;
public class TreeMapDemo {
public static void main (String [] args) {
TreeMap<Integer, String> l = new TreeMap<Integer, String>();
l.put (10, "Java");
l.put (15, "HTML");
l.put (20, "Python");
l.put (25, "JavaScript");
l.put (30, "C++");
System.out.println ("Initial Mappings are: " + l);
TreeMap<Integer, String> t = new TreeMap<Integer, String>();
t.putAll (l);
System.out.println ("The new map: " + t);
} }
Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
The new map: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
- keySet (): It returns the set view of keys in the TreeMap.
Example:
import java.util.*;
public class TreeMapDemo {
public static void main (String [] args) {
TreeMap<Integer, String> l = new TreeMap<Integer, String> ();
l.put (10, "Java");
l.put (15, "HTML");
l.put (20, "Python");
l.put (25, "JavaScript");
l.put (30, "C++");
System.out.println ("Initial Mappings are: " + l);
System.out.println ("The key in the map: " + l.keySet ());
} }
Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
The key in the map: [10, 15, 20, 25, 30]
- Values (): This returns the collections view of values in the TreeMap.
Example:
import java.util.*;
public class TreeMapDemo {
public static void main (String [] args) {
TreeMap<Integer, String> l = new TreeMap<Integer, String> ();
l.put (10, "Java");
l.put (15, "HTML");
l.put (20, "Python");
l.put (25, "JavaScript");
l.put (30, "C++");
System.out.println ("Initial Mappings are: " + l);
System.out.println ("The value in the map: " + l.values ());
} }
Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
The value in the map: [Java, HTML, Python, JavaScript, C++]
- entrySet (): This returns the Set view of the whole mapping in the map.
Example:
import java.util.*;
public class TreeMapDemo {
public static void main (String [] args) {
TreeMap<Integer, String> l = new TreeMap<Integer, String> ();
l.put (10, "Java");
l.put (15, "HTML");
l.put (20, "Python");
l.put (25, "JavaScript");
l.put (30, "C++");
System.out.println ("Initial Mappings are: " + l);
System.out.println ("The value in the map: " + l.entrySet ());
} }
Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
The value in the map: [10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++]
- firstKey (): This returns the first (lowest) key in the sorted TreeMap.
Example:
import java.util.*;
public class TreeMapDemo {
public static void main (String [] args) {
TreeMap<Integer, String> l = new TreeMap<Integer, String> ();
l.put (10, "Java");
l.put (15, "HTML");
l.put (20, "Python");
l.put (25, "JavaScript");
l.put (30, "C++");
System.out.println ("Initial Mappings are: " + l);
System.out.println ("The lowest key in the map: " + l.firstKey ());
} }
Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
The lowest key in the map: 10
- lastKey (): This returns the last (highest) key in the sorted TreeMap.
Example:
import java.util.*;
public class TreeMapDemo {
public static void main (String [] args) {
TreeMap<Integer, String> l = new TreeMap<Integer, String> ();
l.put (10, "Java");
l.put (15, "HTML");
l.put (20, "Python");
l.put (25, "JavaScript");
l.put (30, "C++");
System.out.println ("Initial Mappings are: " + l);
System.out.println ("The highest key in the map: " + l.lastKey ());
} }
Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
The highest key in the map: 30
- remove (Object K): It is used to remove the mapping of the specified key from the TreeMap.
Example:
import java.util.*;
public class TreeMapDemo {
public static void main (String [] args) {
TreeMap<Integer, String> l = new TreeMap<Integer, String> ();
l.put (10, "Java");
l.put (15, "HTML");
l.put (20, "Python");
l.put (25, "JavaScript");
l.put (30, "C++");
System.out.println ("Initial Mappings are: " + l);
System.out.println ("The value removes in the map: " + l.remove (20));
} }
Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
The value removes in the map: Python
- headMap (Object key): This method is used to view the only portion of the map which is less than the parameter key.
Example:
import java.util.*;
public class TreeMapDemo {
public static void main (String [] args) {
TreeMap<Integer, String> l = new TreeMap<Integer, String>();
l.put (10, "Java");
l.put (15, "HTML");
l.put (20, "Python");
l.put (25, "JavaScript");
l.put (30, "C++");
System.out.println ("Initial Mappings are: " + l);
System.out.println ("The value in the map: " + l.headMap (20));
} }
Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
The value in the map: {10=Java, 15=HTML}
- subMap (Object start key, Object endKey): This method is used to view the portion of the map between the ranges of starting point of the key to the end point of key.
Example:
import java.util.*;
public class TreeMapDemo {
public static void main (String [] args) {
TreeMap<Integer, String> l = new TreeMap<Integer, String> ();
l.put (10, "Java");
l.put (15, "HTML");
l.put (20, "Python");
l.put (25, "JavaScript");
l.put (30, "C++");
System.out.println ("Initial Mappings are: " + l);
System.out.println ("The value in the map: " + l.subMap (15, 30));
} }
Output:
Initial Mappings are: {10=Java, 15=HTML, 20=Python, 25=JavaScript, 30=C++}
The value in the map: {15=HTML, 20=Python, 25=JavaScript}