Java TreeMap tailMap(K fromKey) Method Example

The tailMap(K fromKey) method of java.util.TreeMap class returns a view of the portion of this map whose keys are greater than or equal to fromKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports.

Signature

public SortedMap<K,V> tailMap(K fromKey)

This method returns a view of the portion of this map whose keys are greater than or equal to fromKey. The returned map is backed by this map, so changes in the returned map are reflected in this map, and vice-versa. The returned map supports all optional map operations that this map supports. Throws java.lang.ClassCastException  if fromKey is not compatible with this map’s comparator,
java.lang.NullPointerException  if fromKey is null and this map uses natural ordering, or its comparator does not permit null keys and java.lang.IllegalArgumentException  if this map itself has a restricted range, and fromKey lies outside the bounds of the range

Example

The following example shows the usage of java.util.TreeMap.tailMap(fromKey) method.

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

public class TailMapMethodTreeMapExample 
{
    public static void main(String args[])
    {
        // create an empty TreeMap
        TreeMap<Integer,String>  treeMap1 = new TreeMap<Integer,String>();
        SortedMap<Integer, String>  tailMap = new TreeMap<Integer,String>();
        
        // use put() method to put elements to the TreeMap
        treeMap1.put(1,"Element1");
        treeMap1.put(2,"Element2");
        treeMap1.put(3,"Element3");
        treeMap1.put(4,"Element4");
        treeMap1.put(5,"Element5");
        
        //Print the elements of TreeMap 
        System.out.println("**Elements of TreeMap**");
        for(Map.Entry<Integer,String> entry : treeMap1.entrySet())
        {
            System.out.println("Key : "+entry.getKey()+" ...."+"Value : "+entry.getValue());
        }
        //Print the Size of the treemap
        System.out.println("**Size of the treemap is : "+treeMap1.size()+"**");
        
        //Get the tailMap from treeMap1
        tailMap = treeMap1.tailMap(2);
        
        //Print the elements of tailMap
        System.out.println("**Elements of tailMap**");
        for(Map.Entry<Integer,String> entry : tailMap.entrySet())
        {
            System.out.println("Key : "+entry.getKey()+" ...."+"Value : "+entry.getValue());
        }
        //Print the Size of the tailMap
        System.out.println("**Size of the tailMap is : "+tailMap.size()+"**");
     }
}

Output

**Elements of TreeMap**
Key : 1 ....Value : Element1
Key : 2 ....Value : Element2
Key : 3 ....Value : Element3
Key : 4 ....Value : Element4
Key : 5 ....Value : Element5
**Size of the treemap is : 5**
**Elements of tailMap**
Key : 2 ....Value : Element2
Key : 3 ....Value : Element3
Key : 4 ....Value : Element4
Key : 5 ....Value : Element5
**Size of the tailMap is : 4**

Leave a Reply

Your email address will not be published. Required fields are marked *