How to add new elemnt to the Existing TreeSet
Previous | Home | Next |
This example will show how to add the new element to the existing TreeSet.
We use Boolean add( object e) method of TreeSet class to add new element to the Existing TreeSet.
Boolean add( object e) will add the value to the existing values of TreeSet.
This example use the add() method and it will call with the help of an object of the class.
package Example;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.SortedSet;
public class AddSubSet {
/**
* @param args
*/
/**
* @param args
*/
public static void main(String[] args) {
TreeSet Tst =new TreeSet();
Tst.add(new Integer("10"));
Tst.add(new Integer("17"));
Tst.add(new Integer("15"));
Tst.add(new Integer("19"));
Tst.add(new Integer("12"));
Tst.add(new Integer("13"));
boolean sortedSet = Tst.add(new Integer ("16"));
System.out.println(" The elements are:" +Tst);
// TODO Auto-generated method stub
}
}
The elements of the TreeSet are:[10, 12, 13, 15, 17, 19]
After one new element are:[10, 12, 13, 15, 16, 17, 19]
Previous | Home | Next |