What is Set interface?
The set interface present in java.util package extends Collection interface. It is an unordered (Set does not maintain insertion order) collection which does not allow duplicates.
Since Set does not allow duplicates, it is widely used in applications/programs where we must avoid duplicates.
When we want to create a Set, we cannot create object of it directly. We need to use any class which directly or indirectly implements Set. Hashset, LinkedHashSet & TreeSet are the classes which can be used for creating a Set type collection.
Example
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
// Set demonstration using HashSet
Set<String> hash_Set = new HashSet<>();
hash_Set.add("program");
hash_Set.add("to");
hash_Set.add("demonstrate");
hash_Set.add("set");
hash_Set.add("set");
System.out.println(hash_Set);
}
}
Output
[set, program, to, demonstrate]
Explanation
There are 2 things to observe in the above program. First, even if we have tried to insert a duplicate element, it was ignored by Set. Second, the order of insertion is not maintained while printing, which is another property of Set.
Describe different operations possible on a Set?
The following basic operations can be performed on a Set.
•Adding element – We can add new element to a set using add() method. Null is accepted as a value to be added in a Set.
•Remove an element – We can remove an element from a Set using remove() method.
•Verify presence – We can verify presence of an element in a Set using contains() method.
•Iterate – We can iterate thru a Set using for-each loop or Iterator.
Let’s see the above operations with the help of a program.
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<String> hash_Set = new HashSet<>();
hash_Set.add("program");
hash_Set.add("to");
hash_Set.add("demonstrate");
hash_Set.add("set");
hash_Set.add(null);
System.out.println(hash_Set);
// Remove an element
hash_Set.remove(null);
System.out.println(hash_Set);
// Verify presence of an element
System.out.println(hash_Set.contains("program"));
//Iterate thru for each loop
for (String s : hash_Set) {
System.out.print(s + " ");
}
System.out.println();
//Iterate thru Iterator;
Iterator<String> itr = hash_Set.iterator();
while (itr.hasNext()) {
System.out.print(itr.next() + " ");
}
}
}
Output –
[null, set, program, to, demonstrate]
[set, program, to, demonstrate]
true
set program to demonstrate
set program to demonstrate
How can we have an Ordered Set?
We can use LinkedHashSet class to achieve this. LinkedHashSet extends Hashset class, so it inherits all properties of HashSet, but additionally offers ordering of element based on insertion sequence.
Example
import java.util.LinkedHashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<String> linkedhashset = new LinkedHashSet<>();
linkedhashset.add("program");
linkedhashset.add("to");
linkedhashset.add("demonstrate");
linkedhashset.add("linked hash set");
System.out.println(linkedhashset);
}
}
Output
[program, to, demonstrate, linked hash set]
Explanation
The insertion order is maintained while printing the values of the LinkedHashSet.
How can we sort a Set?
There is no direct support to sort elements in a Set. But we can use TreeSet class to achieve this. Let’s see this in an example.
Example
import java.util.Set;
import java.util.TreeSet;
public class Main {
public static void main(String[] args) {
Set<String> treeset = new TreeSet<>();
treeset.add("program");
treeset.add("to");
treeset.add("demonstrate");
treeset.add("tree set");
System.out.println(treeset);
}
}
Output
[demonstrate, program, to, tree set]
Explanation
Even if the inserted words in the TreeSet are not in sorted manner, when we print the elements, they are printed in sorted manner.
How to convert a Set to a List and vice versa?
List to Set Example
public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Asia");
list.add("Asia");
list.add("Europe");
list.add("North America");
Set<String> set = new TreeSet<>();
set.addAll(list);
for (String s : set)
{
System.out.println(s);
}
}
}
Output
Asia
Europe
North America
Explanation
First, we create a List and add few elements in it. Then we create a TreeSet and using addAll() method, we insert all elements of the List to the TreeSet and print the values in the console.
The important thing to observe here is, even though the List have a duplicate item, TreeSet did not allow that. So, we can remove the duplicates of List using this mechanism.
Set to List Example
import java.util.*;
public class Main {
public static void main(String[] args) {
Set<String> set = new HashSet<>();
set.add("123");
set.add("456");
set.add("555");
set.add("421");
List<String> list = new ArrayList<>();
list.addAll(set);
for (String s : list)
{
System.out.println(s);
}
}
}
Output
123
421
456
555