Set Interface and its methods
Before discussing about Set interface we need to know about the two concepts related to Set
- Interface
- Collection
What is an Interface?
Interface is a property in Java which assigns the behavior of class. Interface is of abstract type which consists of only abstract methods thus resulting in 100 percent abstraction. The problems related to multiple level inheritance cannot be solved using class concept since a single child class will be inheriting two parent class thus resulting in “diamond problem”. To overcome the diamond problem ,concept of interface is used so as to solve problems of multiple level inheritance. It is represented by a Java keyword “interface”. To create relation between any class and interface we use “implements” keyword. To create a relation between two interfaces we use “extends” keyword.
Advantages of Interface
- Using interface we can achieve multiple level inheritance
- Since interface consists of only abstract methods, so every abstract gets overridden in the child class resulting in code implementation getting hidden. This characteristic of interface help organisation to hide the sensitive details of the organization.
What is Collection?
Collection is an interface which consists of lot of inbuild classes and interfaces with the help of which multiple datas can be stored. Collection has a relation with an interface called as “Iterable” interface and the relationship is created using “extends” keyword. It is a framework or an entity which has two packages
- java.util.Collection;
- java.util.Map;
Advantages of Collection
Collection has lot of advantages which are as follows:
- Collection can store heterogeneous data
- Collection is dynamic in nature so its size can be altered at runtime as per the requirement .
- Collection API consists of lot of interfaces like List, Set, and each of these interfaces have implemented classes and so the methods of these classes help in code reusability.
- Collection framework reduces the programming effort because of successful implementation of abstraction.
- Collection methods result in best implementation of program code.
Collection Hierarchy
The below diagram shows the Collection hierarchy consisting of classes and interfaces.
As per the scope of the blog our discussion will be limited to the Set interface
What is Set?
Set is an interface which is the child interface of Collection interface and grandchild of Iterable interface. Set is not an index based data structure and all the datas are stored in accordance with the hashcode values. Duplicate elements cannot be stored in Set. Set allows only one null value to be stored in it. Set does not follow the order of insertion. Set elements can be iterated by only Iterator method. The various implemented classes of Set interface are HashSet,LinkedHashSet and TreeSet where HashSet and LinkedHashSet are child classes of Set interface and TreeSet is the great grand child class of Set interface. The various child interfaces associated with Set are SortedSet and Navigation Set where SortedSet is the child interface of Set, Navigation Set is the grandchild interface of Set.
Since Set is an interface we cannot create Object of Set directly but instead we upcast the implemented classes of Set like HashSet, LinkedHashSet etc. with Set in order to create an instance of Set.
Methods of Set interface
Below are some of the methods of Set interface:
Method Name | Description |
add(Object e) | This method is used to add a new element of any datatype into the Set interface. |
addAll(Collection c) | This method is used to add all elements of different datatypes into the Set interface. |
clear() | It is used to clear all the elements from the Set interface but it does not clear the Set interface. |
contains(Object o) | This method is used to check the presence of a Collection element in the Set. It returns ‘true’ value if the element is present in Set and ‘false’ value if the element is not present in the Set. |
containsAll(Collection c) | This method is used to check if the Set contains all the collection elements. It returns ‘true’ value if all the elements are present in Set and ‘false’ value if any one of the element is not present in the Set. |
isEmpty() | This method checks if the given Set is empty |
iterator() | This method is used to iterate sequentially all the Collection elements present in a Set. |
remove(Object o) | This method is used to remove a specified collection element from the Set. |
removeAll(Collection c) | This method removes all those Collection elements from the current Set which are present in another Set. |
retainAll(Collection c) | This method retains all those elements from the current Set which are present in another Set and removes those elements which are present in current Set but not in another Set and vice versa. |
size() | This method is used to find out and display the number of Collection elements present in the Set |
toArray() | This method is used to convert all collection elements present in a Set into an array. |
toString() | This method is used to convert all collection elements present in a Set into a String. |
equals() | This method is used to check if two Sets are equal. It returns ‘true’ value if elements as well as size of elements present in one Set is equal to the elements and its size present in another Set. If the elements and the size of elements present in one Set is not equal to the elements and its size present in another Set then it returns ‘false’ value. |
1. Let us check the usage of add(Object e) method in Set interface.
Code Snippet:
package setInterface;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class SetInterfaceProgram {
public static void main(String[] args) {
Set a1= new HashSet();
a1.add(12);
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
System.out.println(a1);
}
}
Output: [null, c, 3.4, 3.55, hello, 12, true]
Screenshot:
2. Let us check the usage of addAll(Collection c) method in Set interface.
Code Snippet:
package setInterface;
import java.util.HashSet;
import java.util.Set;
public class SetInterfaceProgram {
public static void main(String[] args) {
Set a1= new HashSet();
a1.add(12);
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
Set a2= new HashSet();
a2.addAll(a1);
System.out.println(a2);
}
}
Output: [null, c, 3.4, Hiii, 3.55, hello, 12, true]
Screenshot:
3. Let us check the usage of clear() method in Set interface.
Code Snippet :
package setInterface;
import java.util.HashSet;
import java.util.Set;
public class SetInterfaceProgram {
public static void main(String[] args) {
Set a1= new HashSet();
a1.add(12);
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
a1.clear();
System.out.println(a1);
}
}
Output: []
Screenshot:
4. Let us check the usage of contains(Object o) method in Set interface.
Code Snippet:
package setInterface;
import java.util.HashSet;
import java.util.Set;
public class SetInterfaceProgram {
public static void main(String[] args) {
Set a1= new HashSet();
a1.add(12);
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
System.out.println("The given Set contains: "+a1.contains("hello"));
System.out.println("The given Set contains: "+a1.contains("Harish"));
}
}
Output: The given Set contains: true
The given Set contains: false
Screenshot:
5. Let us check the usage of containsAll(Collection c) method in Set interface
Code Snippet:
package setInterface;
import java.util.HashSet;
import java.util.Set;
public class SetInterfaceProgram {
public static void main(String[] args) {
Set a1= new HashSet();
a1.add(12);
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
Set a2= new HashSet();
a2.add(12);
a2.add("hello");
a2.add('c');
a2.add(3.4f);
System.out.println("Does a2 contains all elements of a1:"+a2.containsAll(a1));
System.out.println("Does a1 contains all elements of a2:"+a1.containsAll(a2));
}
}
Output: Does a2 contains all elements of a1:false
Does a1 contains all elements of a2:true
Screenshot:
6. Let us check the usage of isEmpty() method in Set interface.
Code Snippet:
package setInterface;
import java.util.HashSet;
import java.util.Set;
public class SetInterfaceProgram {
public static void main(String[] args) {
Set a1= new HashSet();
a1.add(12);
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
System.out.println("Is a1 Set empty?: "+a1.isEmpty());
Set a2= new HashSet();
System.out.println("Is a2 Set empty?: "+a2.isEmpty());
}
}
Output: Is a1 Set empty?: false
Is a2 Set empty?: true
Screenshot:
7. Let us check the usage of iterator() method in Set interface.
Code Snippet:
package setInterface;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class SetInterfaceProgram {
public static void main(String[] args) {
Set a1= new HashSet();
a1.add(12);
a1.add(5);
a1.add(32);
a1.add(24);
Iterator a2= a1.iterator();
System.out.println("The collection elements values are: ");
while(a2.hasNext())
{
System.out.println(a2.next());
}
}
}
Output: The collection elements values are:
32
5
24
12
Screenshot:
8. Let us check the usage of remove(Object o) method in Set interface.
Code Snippet:
package setInterface;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class SetInterfaceProgram {
public static void main(String[] args) {
Set a1= new HashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
a1.remove("hello");
a1.remove(null);
System.out.println(a1);
}
}
Output: [c, 3.4, Hiii, 3.55, true]
Screenshot:
9. Let us check the usage of removeAll(Collection c) method in Set interface
Code Snippet:
package setInterface;
import java.util.HashSet;
import java.util.Set;
public class SetInterfaceProgram {
public static void main(String[] args) {
Set a1= new HashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
Set a2= new HashSet();
a2.add(true);
a2.add('c');
a2.add("hello");
a2.add("hi");
a1.removeAll(a2);
System.out.println("Elements present in a1 after removing a2 elements from a1 are:"+a1);
}
}
Output: Elements present in a1 after removing a2 elements from a1 are:[null, 3.4, Hiii, 3.55]
Screenshot:
10. Let us check the usage of retainAll(Collection c) method in Set interface.
Code Snippet:
package setInterface;
import java.util.HashSet;
import java.util.Set;
public class SetInterfaceProgram {
public static void main(String[] args) {
Set a1= new HashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
Set a2= new HashSet();
a2.add(true);
a2.add('c');
a2.add("hello");
a2.add("hi");
a1.retainAll(a2);
System.out.println("Elements present in a1 after retaining a2 elements are:"+a1);
}
}
Output:
Elements present in a1 after retaining a2 elements are:[c, hello, true]
Screenshot:
11. Let us check the usage of size() method in Set interface.
Code Snippet:
package setInterface;
import java.util.HashSet;
import java.util.Set;
public class SetInterfaceProgram {
public static void main(String[] args) {
Set a1= new HashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
System.out.println("The size of the collection elements present in the Set is: "+a1.size());
}
}
Output: The size of the collection elements present in the Set is: 7
Screenshot:
12. Let us check the usage of toArray() method in Set interface.
Code Snippet :
package setInterface;
import java.util.HashSet;
import java.util.Set;
public class SetInterfaceProgram {
public static void main(String[] args) {
Set a1= new HashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
Object a2[]= a1.toArray();
System.out.println("The list of array elements are:");
for(int i=0;i<=a2.length-1;i++)
{
System.out.println(a2[i]);
}
}
}
Output: The list of array elements are:
null
c
3.4
Hiii
3.55
hello
true
Screenshot:
13. Let us check the usage of toString() method in Set interface.
Code Snippet:
package setInterface;
import java.util.HashSet;
import java.util.Set;
public class SetInterfaceProgram {
public static void main(String[] args) {
Set a1= new HashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
System.out.println(a1.toString());
}
}
Output: [null, c, 3.4, Hiii, 3.55, hello, true]
Screenshot:
14. Let us check the usage of equals() method in Set interface.
Code Snippet:
package setInterface;
import java.util.HashSet;
import java.util.Set;
public class SetInterfaceProgram {
public static void main(String[] args) {
Set a1= new HashSet();
a1.add("hello");
a1.add('c');
a1.add(3.4f);
a1.add(true);
a1.add(null);
a1.add(3.55);
a1.add("Hiii");
Set a2= new HashSet();
a2.add("hello");
a2.add('c');
a2.add(3.4f);
a2.add(true);
a2.add(null);
a2.add(3.55);
a2.add("Hiii");
Set a3= new HashSet();
a3.add("hello");
a3.add('c');
a3.add(3.4f);
a3.add(true);
System.out.println("Is Set a1 equal to a2 ?:"+ a1.equals(a2));
System.out.println("Is Set a2 equal to a3 ?:"+ a2.equals(a3));
}
}
Output: Is Set a1 equal to a2 ?:true
Is Set a2 equal to a3 ?:false
Screenshot:
Differences between List Interface and Set Interface
SL NO: | List | Set |
1 | List follows indexing | Set does not follow indexing |
2 | List allows duplicate elements | Set does not allow duplicate elements |
3 | List allows any number of null values | Set allows only one null value |
4 | List follows order of insertion | Set does not follow order of insertion |
5 | We iterate List elements using Iterator and ListIterator | We iterate Set elements only by using Iterator |
6 | ArrayList,LinkedList and Vector are some of the implemented classes of List interface. | TreeSet,HashSet and LinkedHashSet are some of the implemented methods of Set. |
Conclusion
Set interface is used for handling all the Collection elements in an effective way. It is necessary to understand the usage of all the implemented classes and interfaces of Set interfaces as well as its methods for better handling of Collection elements. By thoroughly understanding the Set interface and its methods developers and testers can utitlise those concepts in their real life tasks.
Consult Us