![]() |
VOOZH | about |
In Java, HashSet is a part of the java.util package is used to store a collection of unique elements. It does not allow duplicate values and does not maintain any insertion order. Initializing a HashSet can be done in multiple ways depending on the use case.
In this approach, an array is converted to a list and passed to the HashSet constructor that accepts a collection.
Note: HashSet does not guarantee sorted order; the output order may vary.
[1, 2, 3, 4, 5, 6, 7, 8]
Explanation:
Collections class consists of several methods that operate on collections.
Adds all elements to the specified collection.
[1, 2, 3, 4, 5, 6, 7, 8]
Explanation:
Creates an unmodifiable (read-only) view of the set.
[1, 2, 3, 4, 5, 6, 7, 8]
Explanation:
Create a set and using .add() method we add the elements into the set
[1, 2, 3, 4, 5, 6, 7, 8]
Note: The order of elements in the output may vary because HashSet does not maintain insertion or sorted order.
Explanation: