Java.util - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.util TreeSet

Java.util TreeSet

shape Description

The java.util.TreeSet class actualizes the Set interface. The TreeSet class ensures that the Map will be in climbing key request and supported by a TreeMap. The Map is sorted by normal sort strategy for the key Class, or by the Comparator gave at set creation time, that will rely on upon which constructor utilized. The requesting must be add up to all together for the Tree to work appropriately. The following is the syntax declaration. [java]public class TreeSet<E> extends AbstractSet<E> implements NavigableSet<E>, Cloneable, Serializable[/java]

shape Methods

The following are the various methods of the class.
Methods Description
add(Object o) To add an element.
ceiling(E e) To give back minimal component in this set more noteworthy than or equivalent to the given component
clone() To get the copy of an object.
isEmpty() To check ehther the set is empty or not.

shape Example

The following is an example by using add(). [java]package util; import java.util.Iterator; import java.util.TreeSet; public class TreesetDemo { public static void main(String[] args) { // creating a TreeSet TreeSet <Integer>treeadd = new TreeSet<Integer>(); // adding in the tree set treeadd.add(10); treeadd.add(11); treeadd.add(12); treeadd.add(13); // create an iterator Iterator iterator; iterator = treeadd.iterator(); // displaying the Tree set data System.out.print("Tree set data: "); while (iterator.hasNext()){ System.out.print(iterator.next() + " "); } } }[/java] Output: Now compile the code result will be as follows. [java]Tree set data: 10 11 12 13 [/java] The following is an example by using ceiling() method. [java]package util; import java.util.Iterator; import java.util.TreeSet; public class TreesetDemo { public static void main(String[] args) { // creating a TreeSet TreeSet <Integer>treeadd = new TreeSet<Integer>(); // adding in the tree set treeadd.add(12); treeadd.add(11); treeadd.add(16); treeadd.add(15); // getting ceiling value for 13 System.out.println("Ceiling value for 13: "+treeadd.ceiling(13)); } }[/java] Output: Now compile the code result will be as follows. [java]Ceiling value for 13: 15[/java] The following is an example by using clone(). [java]package util; import java.util.Iterator; import java.util.TreeSet; public class TreesetDemo { public static void main(String[] args) { // creating TreeSet TreeSet <Integer>tree = new TreeSet<Integer>(); TreeSet <Integer>clonetree = new TreeSet<Integer>(); // adding in the tree tree.add(11); tree.add(13); tree.add(14); // cloning tree into clinetree clonetree = (TreeSet)tree.clone(); // creating iterator Iterator iterator; iterator = clonetree.iterator(); // displaying the clonetree data System.out.println("Tree set data: "); while (iterator.hasNext()){ System.out.println(iterator.next() + " "); } } }[/java] Output: Now compile the code result will be as follows. [java] Tree set data: 11 13 14 [/java]

Summary

shape Key Points

  • The pollFirst() method will get and remove the first element.
  • The Object class is the parent class for all the child classes.
  • The tailSet(E fromElement) is used to view the particular area of the set.