TreeSet Java Add Print Clone Elements

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.

TreeSet Java

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.

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.

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.

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

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.

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“.

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.

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.

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. Siddu

    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 Reply

Your email address will not be published. Required fields are marked *