Java TreeSet.subSet(E fromElement, E toElement)

Syntax

TreeSet.subSet(E fromElement, E toElement) has the following syntax.

public SortedSet <E> subSet(E fromElement,   E toElement)

Example

In the following code shows how to use TreeSet.subSet(E fromElement, E toElement) method.


//from  www .j  av  a 2s .  c o m

import java.util.TreeSet;
import java.util.Iterator;

public class Main {
   public static void main(String[] args) {
      
      TreeSet <Integer> treeadd = new TreeSet<Integer> ();
     
      treeadd.add(1);
      treeadd.add(2);
      treeadd.add(3);
      treeadd.add(4);
      treeadd.add(5);
      treeadd.add(6);
      treeadd.add(7);
      treeadd.add(8);
      
      TreeSet<Integer> treesubset=(TreeSet<Integer>)treeadd.subSet(3,7); 
      
      Iterator<Integer> iterator = treesubset.iterator();
     
      while (iterator.hasNext()){
         System.out.println(iterator.next());
      }
   }    
}

The code above generates the following result.