The java.util.TreeSet.add() method
The method add(Object o) is used to add an element to the TreeSet if it does not exist in the list.
Here's how to declare the java.util.TreeSet.add():
public boolean add(Object o): This method returns true if the TreeSet does not contain this element and is inserted successfully.
Here's how to declare the java.util.TreeSet.add():
public boolean add(Object o): This method returns true if the TreeSet does not contain this element and is inserted successfully.
Example
The following example shows the use of the java.util.TreeSet.add():
import java.util.Iterator;Let's compile and run the above program, this will produce the following result:
import java.util.TreeSet;
public class Treeset {
public static void main(String[] args) {
// creating a TreeSet
TreeSettreeadd = new TreeSet ();
// add values to TreeSet
treeadd.add(54);
treeadd.add(40);
treeadd.add(96);
treeadd.add(15);
treeadd.add(34);
// create an iterator to browse TreeSet
Iterator iterator = treeadd.iterator();
// display all TreeSet elements
System.out.println("TreeSet elements");
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
TreeSet15
34
40
54
96
References:
Javadoc: TreeSet add() method
Javadoc: TreeSet add() method