TreeSet add Example


TreeSet class add example.boolean add(Object e) Adds the specified element to this set if it is not already present.

Here is the code

/**
 * @ # Add.java
 * A class repersenting use to add method 
 * of TreeSet class in java.util package
 * version 23 May 2008
 * author Rose India 
 */
import java.util.*;

public class Add {

    public static void main(String[] args) {

        // Create a new TreeSet object:
        TreeSet ts = new TreeSet();

        // Create new element objects:
        Integer x = new Integer(5);
        Integer y = new Integer(4);
        Integer z = new Integer(4);

        // Add the elements to the set and display it:
        System.out.println("Element is added :" + ts.add(x));
        System.out.println("Element is added :" + ts.add(y));
        System.out.println("Element is added :" + ts.add(z));
        
        
        System.out.println("ts = " + ts);
    }
}

Output
 Element is added :true
Element is added :true
Element is added :false
ts = [4, 5]

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US