Answers for "TreeSet headSet(E toElement boolean inclusive) method in java"

2

TreeSet headSet(E toElement) method in java

import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetHeadSetEToElementMethodExample
{
   public static void main(String[] args)
   {
      TreeSet<Integer> ts = new TreeSet<Integer>();
      TreeSet<Integer> hs = new TreeSet<Integer>();
      ts.add(1);
      ts.add(2);
      ts.add(3);
      ts.add(4);
      ts.add(5);
      ts.add(6);
      // get values less than 5
      hs = (TreeSet)ts.headSet(5);
      // create iterator
      Iterator<Integer> iterate = hs.iterator();
      System.out.println("TreeSet data: ");
      while(iterate.hasNext())
      {
         System.out.println(iterate.next() + " ");
      }
   }
}
Posted by: Guest on October-26-2020
2

TreeSet headSet() method in java

import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetHeadSetMethodExample
{
   public static void main(String[] args)
   {
      TreeSet<Integer> ts = new TreeSet<Integer>();
      TreeSet<Integer> hs = new TreeSet<Integer>();
      ts.add(1);
      ts.add(2);
      ts.add(3);
      ts.add(4);
      ts.add(5);
      ts.add(6);
      // get values for 5 inclusive true
      hs = (TreeSet)ts.headSet(5, true);
      // create iterator
      Iterator<Integer> iterate = hs.iterator();
      System.out.println("TreeSet data for '5' inclusive TRUE: ");
      while(iterate.hasNext())
      {
         System.out.println(iterate.next() + " ");
      }
      // get values for 5 inclusive false
      hs = (TreeSet)ts.headSet(5, false);
      iterate = hs.iterator();
      System.out.println("TreeSet data for '5' inclusive FALSE: ");
      while(iterate.hasNext())
      {
         System.out.println(iterate.next() + " ");
      }
   }
}
Posted by: Guest on October-26-2020

Code answers related to "TreeSet headSet(E toElement boolean inclusive) method in java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language