CodingBison

A Set interface is one of the widely used Collection interfaces. As shown in the figure below, set interface extends Collection interface. Hence, it inherits all the operations from Collection interface.



Figure: Set Interface and Class Hierarchy

How different is Set compared to other Collections? Here is the answer. Set does not allows duplicate entries, this means a Set cannot have 2 objects say, obj1 and obj2 where obj1.equals(obj2) is true. As shown in the below figure, there are 2 classes (HashSet and AbstractSet) that implements the interface Set. HashSet also has a another sub-class called LinkedHashSet. We will see each of these classes in detail as we go further.

Methods defined in Set interface

Things To Remember
Set does not allows duplicate elements (Unlike List) and it imposes this restriction on all of its methods.

The Set interface inherits all the methods from the Collection interface, but as we know Set has its own constraints/properties that are different from the other Collection interfaces. So, Set interface add those constrains to these methods, on top of the ones inherited from Collection interface.

For instance, one of the constrains that a Set interface imposes on the methods that it inherited from Collection Interface is, with respect to add/addAll operations. As we know Set does not allow duplicate elements. Therefore, add/addAll methods adds an object only if it is not already present. In addition to the constraints on the methods, it also imposes additional constraints on Constructor of the implementation classes, so the "Set", that the constructors create, must not contain duplicate elements.

Set interface does not define any new methods, it contains all the methods inherited from the Collection interface. The below table represents the methods (with any specific constrains imposed by Set interface) of the Set interface, these methods are quite similar to the methods defined in Collection Interface Methods list.


Table: Generic Methods of Set Interface
MethodDescription
bool add(Object o)Adds an object "o" to the Set if it is not already present. Returns True on Success, False on Failure. Note: Some Set implementations does not allow NULL elements. When an application tries to attempt an operation that is no permitted, it throws an exception.
bool addAll(Collection c)Adds all the objects of collection "c", to the Set (Only if they are not already present) that invokes this method. Returns True on Success, False on Failure. Note: Some Set implementations does not allow NULL elements. When an application tries to attempt an operation that is no permitted, it throws an exception.
bool remove(Object o)Removes an object "o" from the Set (If the element present) that invokes this method. Returns True on Success, False on Failure. Note: Some Set implementations does not allow NULL elements. When an application tries to attempt an operation that is no permitted, it throws an exception.
bool removeAll(Collection c)Removes all the objects of collection "c", from the Set (If the element present) that invokes this method. Returns True on Success, False on Failure. Note: Some Set implementations does not allow NULL elements. When an application tries to attempt an operation that is no permitted, it throws an exception.
int size()Returns the size (aka Cardinality in Set terminology) if the Set that invokes this method.
bool equals(Object o)Compares if object "o" is equal to the object that invokes this method. For Example, a.equals(o); " Where a and o are object (Could be collection objects, Set object, List object etc)". equals method just compares 2 Set objects and returns true if they are both Sets, Sets are of same size and every member of set1 should be present in set2

Here is an Example program that demonstrates the usage of all the above mentioned operations. The example used ArrayList, HashSet and demonstrated the usage of the methods defined by the Set interface. As shown in the Figure at the top of this page, HashSet is one of the Classes that implements the Set interface.

 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Set;

 public class SetInterfaceDemo {
     public static void main(String[] args) {
         Set<Integer> asetObj1 = new HashSet<Integer>();
         asetObj1.add(1);
         asetObj1.add(2);
         asetObj1.add(2);
         asetObj1.add(3);

         ArrayList<Integer> alistObj1 = new ArrayList<Integer>();
         alistObj1.add(1);
         alistObj1.add(33);
         alistObj1.add(33);
         System.out.println("Array List, Duplicates allowed:" + alistObj1);

         HashSet asetObj2 = new HashSet();
         asetObj2.addAll(alistObj1);
         System.out.println("Set 1: "+ asetObj1 + "\nSet 2:" + asetObj2);

         asetObj1.remove(3);
         System.out.println("After removing element 3, Set 1:"+ asetObj1);

         asetObj1.removeAll(asetObj2);
         System.out.println("After removing Set 2 From Set 1:");
         System.out.println("\tS1:" + asetObj1 + "\n\tS2:" + asetObj2);

         boolean status = asetObj1.equals(asetObj2);
         System.out.println("Set 1 and Set 2, Equal ?: " + status);
     }
 }

The above program demonstrates quite a few important properties of the Set interface. If you notice, "asetObj1.add(2);", the program tries to add the same element "2" more than once to the set. But, as we know a set cannot contain duplicate elements and hence the add operation, adds the element only once to the set. The same add() method, if it were to be invoked by an ArrayList object, would add the same element any number of times. Also, notice the addAll() method, the duplicate elements of the ArrayList are not added to the Set. This can be noticed in the output below.

Let us Compile/run the program, Here is the output:

 Array List, Duplicates allowed:[1, 33, 33]
 Set 1:[1, 2, 3]
 Set 2:[1, 33]
 After removing element 3, Set 1:[1, 2]
 After removing Set 2 From Set 1:
 	S1:[2]
 	S2:[1, 33]
 Set 1 and Set 2, Equal ?: false




comments powered by Disqus