Get Tail Map from TreeMap - Java Collection Framework

Java examples for Collection Framework:TreeMap

Introduction

To get a Tail Map from Java TreeMap use, SortedMap tailMap(Object fromKey) method of Java TreeMap class.

It returns the portion of TreeMap whose keys are greater than or equal to fromKey.

Demo Code

 
import java.util.SortedMap;
import java.util.TreeMap;
 
public class Main {
 
  public static void main(String[] args) {
 
    //create TreeMap object
    TreeMap treeMap = new TreeMap();
   /* w ww .  j  a  v  a  2 s  .  c  om*/
    //add key value pairs to TreeMap
    treeMap.put("1","One");
    treeMap.put("3","Three");
    treeMap.put("2","Two");
    treeMap.put("5","Five");
    treeMap.put("4","java2s.com");
 
    SortedMap sortedMap = treeMap.tailMap("2");
   
    System.out.println("Tail Map Contains : " + sortedMap);
   
  }
}

Result


Related Tutorials