TreeSet Java Add Print Clone Elements


Two Programs exists on TreeSet Java.

  1. TreeSet General: General operations like printing the elements in natural order (the default behavior of SortedSet, the super class of TreeSet), extracting a part of a set, creating generics TreeSet Java etc. are performed.
  2. TreeSet Special: Operations like cloning the TreeSet, iterating elements with Iterator and the methods like retainAll(), addAll(), containsAll(), size(), remove(), clear() and equals() are performed. The operations are done on a generics TreeSet that stores strings.

This is the second of the above two TreeSet Java examples.

Note: It is advised to read before going through this program, TreeSet Tutorial, to get acquainted with the methods and features of TreeSet Java.

import java.util.*;
public class TreeSetSpecial
{
  public static void main(String args[])
  {
    TreeSet ts1 = new TreeSet();
    ts1.add("one");
    ts1.add("two");
    ts1.add("five");
    ts1.add("six");
    ts1.add("zero");
		     // USING ADDALL
    TreeSet ts2 = new TreeSet();
    ts2.addAll(ts1);
    if(ts1.equals(ts2))
    {
      System.out.println("\nts1 and ts2 contains the same elements.");
    }

    System.out.println("ts1 and ts2 contains the same elements: " + ts1.containsAll(ts2));

    TreeSet ts3 = new TreeSet();
    ts3.add("two");
    ts3.add("six");

		     // USING RETAINALL
    System.out.println("\nts3 elements: " + ts3);
    System.out.println("ts1 before retainAll(): " + ts1 + " and size is " + ts1.size());
    ts1.retainAll(ts3);
    System.out.println("ts1 after retainAll(): " + ts1 + " and size is " + ts1.size());

		     // USING ITERATOR
    System.out.print("\nPrinting the ts3 elements with Iterator: ");
    Iterator it1 = ts3.iterator();
    while(it1.hasNext())
    {
      System.out.print(it1.next() + " ");
    }
		    // CLONING
    Object obj = ts1.clone();
    TreeSet ts4 = (TreeSet) obj;
    System.out.println("\n\nElements of cloned ts4: " + ts4);

    String str = ts4.toString();
    System.out.println("Element of ts4 as string: " + str);

		// REMOVE METHODS
    ts4.remove("six");
    System.out.println("\nts4 after remove(\"six\"): " + ts4);
    ts4.clear();  
    System.out.println("ts4 after clear(): " + ts4);   
  }
}         

TreeSet Java

      TreeSet ts1 = new TreeSet();
      ts1.add("one");

A generics TreeSet ts1 is created and added five elements with add() method, inherited from Collection interface, the super interface of all data structures of Java.

     TreeSet ts2 = new TreeSet();
     ts2.addAll(ts1);
     if(ts1.equals(ts2))
     {
       System.out.println("\nts1 and ts2 contains the same elements.");
     }

Another TreeSet object ts2 is created with all the methods of ts1. All the ts1 elements are added at a stroke with addAll() method, inherited from Collection interface. Now the ts1 and ts2 contains the same elements and it is checked with equals() method. The equals() method, inherited from Collection, returns true if both the collection classes contain the same elements.

 
     System.out.println("ts1 and ts2 contains the same elements: " + ts1.containsAll(ts2));

The containsAll() method returns true if ts1 contains all the elements of ts2. ts1 may have more elements but should contain all the elements of ts2.

     TreeSet ts3 = new TreeSet();
     ts3.add("two");
     ts3.add("six");

Another TreeSet object ts3 is created and added two elements "two" and "six";

     ts1.retainAll(ts3);

The retainAll() method, inherited from Collection, deletes all the elements from ts1 that are not there in ts3. That is, retains only the elements in ts1 that are contained in ts2.

     Iterator it1 = ts3.iterator();
     while(it1.hasNext())
     {
       System.out.print(it1.next() + " ");
     }

The iterator() method, inherited from Collection interface, returns an object of Iterator. The internal working nature of hasNext() and next() methods is clearly explained in “Interface Iterator“.

      Object obj = ts1.clone();
      TreeSet ts4 = (TreeSet) obj;

The clone() method, inherited from Object class, is used to clone ts1. The clone() method returns an object Object class is explicitly type casted to TreeSet ts4. The ts1 and ts4 occupies different locations and maintains perfect encapsulation. The object copying styles are explained in shallow copying, deep copying and cloning.

     String str = ts4.toString();

toString() method, inherited from Object class, is used to convert ts4 into string form. Sometimes, it is required in Java as in displaying the values in a text field. The TextField's setText() method requires a string as a parameter.

     ts4.remove("six");
     ts4.clear();  

The remove("six") method deletes "six" element and clear() method deletes all the elements. After calling clear(), if size() method is called, it prints 0.

2 thoughts on “TreeSet Java Add Print Clone Elements”

  1. The strange thing is that RuntimeException is itself subclass of Exception i.e. all Unchecked exception classes should have been checked exceptions implicitly, BUT they are not.
    How the compiler ignores the unchecked Exception since unchecked exception is the one indrectly sub class of Exception.

Leave a Comment

Your email address will not be published.