TreeSet subSet() method in java code example

Example: TreeSet subSet() method in java

import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetSubSetMethodExample
{
   public static void main(String[] args)
   {
      TreeSet<Integer> ts = new TreeSet<Integer>();
      TreeSet<Integer> sub = new TreeSet<Integer>();
      ts.add(11);
      ts.add(22);
      ts.add(33);
      ts.add(44);
      ts.add(55);
      ts.add(66);
      ts.add(77);
      ts.add(88);
      // creating subset
      sub = (TreeSet)ts.subSet(22, true, 77, true);
      // create iterator
      Iterator<Integer> iterate = sub.iterator();
      System.out.println("TreeSet subset: ");
      while(iterate.hasNext())
      {
         System.out.println(iterate.next() + " ");
      }
   }
}

Tags:

Misc Example