Get Tail Set from TreeSet - Java Collection Framework

Java examples for Collection Framework:TreeSet

Introduction

To get a Tail Set from Java TreeSet use, SortedSet tailSet(Object fromElement) method of Java TreeSet class.

Demo Code

 
import java.util.SortedSet;
import java.util.TreeSet;
 
public class Main {
 
  public static void main(String[] args) {
    TreeSet tSet = new TreeSet();
   //from  w ww  . ja  va 2 s.com
    //add elements to TreeSet
    tSet.add("1");
    tSet.add("3");
    tSet.add("2");
    tSet.add("5");
    tSet.add("4");
 
    SortedSet sortedSet = tSet.tailSet("2");
   
    System.out.println("Tail Set Contains : " + sortedSet);
   
  }
}

Result


Related Tutorials