The Set is an interface in Java that inherits the Collection interface. It is very similar to List, except that Set implementations cannot contain duplicates, while lists can.
That is also the main difference. Wherever we need a collection with unique elements, we will use Set, while in all other cases, we will probably choose List.
Most used classes that implement the Set interface:
- HashSet
- LinkedHashSet
- TreeSet
What is a HashSet in Java?
HashSet class is an implementation of the Set interface and it cannot guarantee the order of the elements.
How to create a HashSet?
We create a new HashSet object by instantiating it, just like any other class in Java.
HashSet<String> set = new HashSet<>();
Between the <> brackets, we specify which type of elements the Set will contain.
Or using polymorphism.
The variable that holds the address of the new object will be of type Set.
This is possible because HashSet implements the Set interface.
Set<String> set = new HashSet<>();
How to add elements to HashSet in Java?
We add elements using the add() method that the HashSet class inherits from the Collection interface.
Example:
class Test { public static void main(String[] args) { Set<String> set = new HashSet<>(); set.add("Java"); set.add("Kotlin"); set.add("Python"); set.add("Ruby"); System.out.println(set); } }
Pay attention to the output, the elements that are printed are not in any order, not even in the order of insertion into the set.
Using the addAll() method, we can add all the elements of an existing set to the newly created set.
class Test { public static void main(String[] args) { Set<String> set = new HashSet<>(); set.add("Kotlin"); set.add("Java"); set.add("Ruby"); set.add("Python"); Set<String> set2 = new HashSet<>(); set2.addAll(set); System.out.println(set2); } }
How to remove elements from HashSet?
We can remove set elements in three ways:
By element
We can use the remove(Object o) method and specify the exact element we want to remove from the set.
Example:
class Test { public static void main(String[] args) { Set<String> set = new HashSet<>(); set.add("Kotlin"); set.add("Java"); set.add("Ruby"); set.add("Python"); set.remove("Ruby"); System.out.println(set); } }
Removing all elements that are present in the specified collection
We can use removeAll(Collection<?> c) to remove all elements present in the collection we passed in as a parameter.
class Test { public static void main(String[] args) { Set<String> set = new HashSet<>(); set.add("Java"); set.add("Kotlin"); Set<String> set2 = new HashSet<>(); set2.add("Java"); set2.add("Python"); set2.add("Kotlin"); set2.add("Ruby"); set2.removeAll(set); System.out.println(set2); } }
Clear the set / remove all elements from the set
We can use the clear() method to remove all elements from the set.
Example:
class Test { public static void main(String[] args) { Set<String> set = new HashSet<>(); set.add("Java"); set.add("Python"); set.add("Kotlin"); set.add("Ruby"); set.clear(); System.out.println(set); } }
Hence, we don’t have the get(int index) or remove(int index) method in Set.
How to get the size of the HashSet in Java?
To find out the size of the HashSet in Java, we use the size() method.
Example:
class Test { public static void main(String[] args) { Set<String> set = new HashSet<>(); set.add("Java"); set.add("Python"); set.add("Kotlin"); set.add("Ruby"); System.out.println("Size of the set: " + set.size()); } }
What is a LinkedHashSet in Java?
LinkedHashSet class inherits the HashSet class and implements the Set interface.
The difference between HashSet and LinkedHashSet is that LinkedHashSet guarantees that the elements will have an insertion order.
Example:
class Test { public static void main(String[] args) { Set<String> set = new LinkedHashSet<>(); set.add("Java"); set.add("Python"); set.add("Kotlin"); set.add("Ruby"); System.out.println(set); } }
What is a TreeSet in Java?
TreeSet class implements the Set interface that uses a tree for storage and it guarantees that the elements will be in ascending order.
Example:
class Test { public static void main(String[] args) { Set<String> set = new TreeSet<>(); set.add("Java"); set.add("Ruby"); set.add("Kotlin"); set.add("Python"); System.out.println(set); } }
How to iterate over Set in Java?
Since we don’t have the get() method in Set, we can’t use the classic for loop. Instead, we can use an Iterator and for-each loop.
Iterate over Set using Iterator
Example:
class Test { public static void main(String[] args) { Set<String> set = new HashSet<>(); set.add("Java"); set.add("Ruby"); set.add("Kotlin"); set.add("Python"); Iterator<String> itr = set.iterator(); // Iterating over Set using Iterator while (itr.hasNext()) { System.out.println(itr.next()); } } }
Iterate over Set using For-each loop
Example:
class Test { public static void main(String[] args) { Set<String> set = new HashSet<>(); set.add("Java"); set.add("Ruby"); set.add("Kotlin"); set.add("Python"); for(String element : set) { System.out.println(element); } } }
Iterate over Set using Java 8 forEach() method
The forEach() method belongs to the Iterable interface, and since Set implements Iterable, we can use it to traverse over Set.
Example:
class Test { public static void main(String[] args) { Set<String> set = new HashSet<>(); set.add("Java"); set.add("Ruby"); set.add("Kotlin"); set.add("Python"); set.forEach(element -> System.out.println(element)); } }