TreeSet Example

TreeSet Example

Previous Home Next

 

TreeSet is one of the two sorted collections. the other is TreeMap
This Example shows the simple program of TreeSet.

TreeSet is the sorted collections.This Example shows how to use TreeSet in java.
 In this Example boolean add(object obj) of TreeSet Class is used to add Elements to  the TreeSet.

create Object of TreeSet .To add element to the TreeSet used Boolean Add (Object obj) method.This method returns true if element added to the TheeSet otherwise return false.

 

package Example;
import java.util.TreeSet;

public class TreeSetExample {


public static void main(String[] args) {
TreeSet Tst =new TreeSet();
Tst.add(new Integer("11"));
Tst.add(new Integer("12"));

System.out.println(" The Elements in the TreeSet are:" +Tst);


}

}

 The Elements in the TreeSet are:[11, 12]

Previous Home Next