Advertisement



< Prev
Next >



Java - TreeMap Class





The TreeMap class extends the AbstractMap abstract class and it also implements the NavigableMap interface. By default, TreeMap stores key-value pairs in a sorted ascending order(based on the key).

The retrieval speed of an element out of a TreeMap is fast, even in a TreeMap with a large number of elements. Hence, TreeMap is the best candidate for a situation where one needs to store a large number of sorted elements in the form of key-value pairs and their fast access.




Important features of TreeMap







A simple constructor of TreeMap


TreeMap()
This constructor creates an empty TreeMap to hold a particular type of key-value pair objects.
Example -
TreeMap <String,Integer> tm = new TreeMap <String,Integer>();

This constructor creates a TreeMap to hold key-value pairs of String and Integer objects.




Some important methods of TreeMap class


Methods Description
void clear() Removes all the key/value pairs from the TreeMap.
int size() Returns the total number of key-value pairs in a TreeMap.
boolean isEmpty() Returns true if TreeMap has no key-value pair in it.
V get(Object key) Returns the value associated with a specified key.
put(K key, V value) Puts the specified key and its associated value in the TreeMap.
K higherKey(K key) Returns a key just greater than the specified key.
K lowerKey(K key) Returns a key just smaller than the specified key.
Set<Map.Entry<K,V>> entrySet() Returns the Set containing Map.Entry objects.





TreeMap example


We are creating a TreeMap to store objects in the pair of key and value, where key is a String object and value is an Integer object. By default, the key-value pairs will be stored in an ascending order of its keys(based on the first character of the String). We will also obtain a value, by specifying its key.

//Java - Example of TreeMap


import java.util.*;


class TreeMapDemo
{
public static void main(String... ar)
{
TreeMap<String,Integer> tm = new TreeMap<String,Integer>();

tm.put("Tom", 14);
tm.put("Rick", 17);
tm.put("Ana", 15);
tm.put("Max", 20);
tm.put("Ian", 19);

System.out.println("TreeMap contents in an ascending order of its keys-" +tm);

System.out.println("Value at the key Ian is "+ tm.get("Ian"));
System.out.println("Value at the key Max is "+ tm.get("Max"));

System.out.println("Key greater than Ana is ")+ tm.higherKey("Ana");
System.out.println("Key smaller than Rick is ")+ tm.lowerKey("Rick");

}
}


Output is :


TreeMap contents in an ascending order of its keys-{Ana=15, Ian=19, Max=20 Rick=17, Tom=14}
Value at the key Ian is 19
Value at the key Max is 20
Key greater than Ana is Ian
Key smaller than Rick is Max


Program Analysis





Advertisement




Iterating over entries in a TreeMap using entrySet() method


In the next example, we are creating another TreeMap to store objects in the pair of key and value, where key is a String object and value is an Integer object. Next, will use the entrySet() method to get a Set view of a TreeMap and eventually we will iterate over each map entry(key-value pair) by using the for-each loop.

//Java - Iterating over entries in a TreeMap by using the entrySet() method


import java.util.*;


class TreeMapDemo
{
public static void main(String... ar)
{
TreeMap<String,Integer> tm = new TreeMap<String,Integer>();

tm.put("Tom", 14);
tm.put("Rick", 17);
tm.put("Ana", 15);
tm.put("Max", 20);
tm.put("Ian", 19);


System.out.println("Iterating TreeMap using Map.Entry and for-each loop");
Set<Map.Entry<String,Integer>> set = tm.entrySet();


for(Map.Entry <String,Integer> me : set)
{
	System.out.print(getKey() +":");
	System.out.println(getValue());
}

}

}


Output is :


{Ana=15, Ian=19, Max=20 Rick=17, Tom=14}
Iterating TreeMap using Map.Entry and for-each loop
Ana:15
Ian:19
Max:20 
Rick:17
Tom:14


Program Analysis






Please share this article -




< Prev
Next >
< HashMap Class
LinkedHashMap Class >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement