how does treeset work in java code example
Example 1: TreeSet isEmpty() method in java
import java.util.TreeSet;
public class TreeSetIsEmptyMethodExample
{
public static void main(String[] args)
{
TreeSet<String> ts = new TreeSet<String>();
ts.add("violet");
ts.add("green");
ts.add("red");
ts.add("yellow");
ts.add("blue");
ts.add("indigo");
System.out.println("Given TreeSet: " + ts);
System.out.println("Is TreeSet empty? " + ts.isEmpty());
ts.clear();
System.out.println("Is TreeSet empty? " + ts.isEmpty());
}
}
Example 2: 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);
sub = (TreeSet)ts.subSet(22, true, 77, true);
Iterator<Integer> iterate = sub.iterator();
System.out.println("TreeSet subset: ");
while(iterate.hasNext())
{
System.out.println(iterate.next() + " ");
}
}
}