
Set is an collection which doesn’t contain duplicate elements unlike ArrayList which may contain dupes. So this makes set unique and different from other collections.
So Set is an interface which extends the Collection interface.
There are 3 implementations of Set:
1)HashSet : This is the best performing Set implementation. It doesn’t maintain the insertion order and neither it sorts the elements in ascending order.
2)TreeSet: This implementation of Set orders the elements according to their values.It stores the elements internally in a red black tree.
3)LinkedHashSet:It is implemented as a hash table with a linked list running through it, orders its elements based on the order in which they were inserted into the set (insertion-order).
Consider an Example. Suppose you want to remove duplicates from an ArrayList named as list. It contains a lot of dup strings. The same can be done easily by using a set as below.
Collection<String> uniqueList= new HashSet<String>(list);
The HashSet uniqueList will contain the unique elements.
Let’s see an example of elements getting inserted into an set collection.
import java.util.*; public class SetExample { public static void main(String[] args) { Set<String> s1 = new HashSet<String>(); for (String str : args) s1.add(str); //line 7 System.out.println(s1.size() + " distinct words: " + s1); } }
If you see line 7, there is an add method.
When the add method is called, it returns true or false. True on successful insertion and false when the element is already present i.e; a duplicate.
Let’s see some important methods of the Set Interface.
1) add( )
Adds an object to the collection.
2)clear( )
Removes all objects from the collection.
3)contains( )
Returns true if a specified object is an element within the collection.
4)isEmpty( )
Returns true if the collection has no elements.
5)iterator( )
Returns an Iterator object for the collection, which may be used to retrieve an object.
6)remove( )
Removes a specified object from the collection.
7)size( )
Returns the number of elements in the collection.
Now suppose, instead of a string there is an User Class Object like below.
Class Employee
{
private string name = null;
private string email = null;
public void setName(String name)
{
this.name = name;
}
public String setEmail(String email)
{
this.email = email;
}
public static void main(String args[])
{
Employee e1 = new Employee();
Employee e2 = new Employee();
e1.setName("John");
e2.setName("John");
e1.setEmail("John@abc.com");
e2.setEmail("John@abc.com");
Set<Employee> s1 = new HashSet<Employee>();
s1.add(e1);
s1.add(e2);
System.out.println("The size of the Set is "+s1.size());
}
}
If you see the above example, we are inserting 2 objects into set. Both have the same values in their member variables but still both get inserted and output is printed as 2.Set allowed dupes here.
Why?
Because we didn’t override the equals and hashCode methods for the Employee class. The hashCode and reference of both the objects are different in spite of the class variable values being same. So set considers them different. To resolve this, we need to override the equals and hashCode methods for Employee class.
Operations on Set Interface:
The set interface allows the users to perform the basic mathematical operations on the set. Lets take and example . Let set1 = [1, 3, 2, 4, 5] and set2 = [1, 2,3,7,8]. Then the possible operations on the sets are:
1. Intersection:Â This operation returns all the common elements from the given two sets. For the above two sets, the intersection would be:
[1,3,2]
2. Union:Â This operation adds all the elements in one set with the other. The union for above case would be:
[1,2,3,4,5,7,8]
3. Difference:Â This operation removes all the values present in one set from the other set. The difference for above case would be:
[4,5]
Let’s look at the code.
import java.util.*;
public class SetOperationExample
{
public static void main(String args[])
{
Set<Integer> a = new HashSet<Integer>();
a.addAll(Arrays.asList(new Integer[] {1, 3, 2, 4, 5}));
Set<Integer> b = new HashSet<Integer>();
b.addAll(Arrays.asList(new Integer[] {1, 2, 3, 7, 8}));
// Union
Set<Integer> union = new HashSet<Integer>(a);
union.addAll(b);
System.out.print("Union of the two Set");
System.out.println(union);
// Intersection
Set<Integer> intersection = new HashSet<Integer>(a);
intersection.retainAll(b);
System.out.print("Intersection of the two Set");
System.out.println(intersection);
// Difference
Set<Integer> diff = new HashSet<Integer>(a);
diff.removeAll(b);
System.out.print("Difference of the two Set");
System.out.println(diff);
}
}
O/P:
[1,2,3,4,5,7,8]
[1,3,2]
[4,5]