Welcome to this lesson on Java's HashSet
. We'll delve into HashSet
as if it were a unique collection of space artifacts where duplicates aren't allowed! By the end, you’ll understand how to work with HashSet
, and you'll know when to use them instead of other structures like arrays and ArrayLists.
HashSet
, which is a part of Java's Collection framework, behaves like a set of unique elements. However, underneath the surface, its mechanism resembles that of HashMap's. Just like a locker in a spaceship that houses only exclusive components, a HashSet
only stores unique elements.
Constructing and adding the names of space rocks to our HashSet
is a straightforward process, thanks to the new HashSet<>()
and add()
methods:
Java1HashSet<String> spaceObjects = new HashSet<>(); 2spaceObjects.add("Asteroid"); 3spaceObjects.add("Meteor"); 4spaceObjects.add("Comet"); 5System.out.println(spaceObjects); // Output: [Asteroid, Comet, Meteor] 6 7spaceObjects.add("Meteor"); // adding a duplicate element 8System.out.println(spaceObjects); // Output: [Asteroid, Comet, Meteor] 9// The duplicate element wasn't added
The first three lines add three different strings to the HashSet
. If we attempt to add "Meteor"
again, it won't have any effect since duplicates aren't permitted, as shown in the example.
HashSet
doesn't allow direct access to elements by index or whatsoever. However, we can search for a specific element using the contains()
method:
Java1boolean isCometPresent = spaceObjects.contains("Comet"); 2System.out.println("Is Comet present in the set? " + isCometPresent); // Output: "Is Comet present in the set? true"
isCometPresent
will be true
as "Comet"
is a part of the hash set we created earlier. You can remove an element through remove()
and use isEmpty()
and size()
methods to inspect the state of the HashSet
.
Here is an example:
Java1HashSet<String> spaceObjects = new HashSet<>(); 2spaceObjects.add("Asteroid"); 3spaceObjects.add("Meteor"); 4spaceObjects.add("Comet"); 5 6System.out.println("Is Comet present in the set? " + spaceObjects.contains("Comet")); 7// Output: "Is Comet present in the set? true" 8spaceObjects.remove("Comet"); 9System.out.println("Is Comet present in the set? " + spaceObjects.contains("Comet")); 10// Output: "Is Comet present in the set? false" 11 12System.out.println("Is set empty? " + spaceObjects.isEmpty()); // Output: "Is set empty? false" 13System.out.println(spaceObjects.size()); // Output: 2
After the above operations, "Comet" is removed from the HashSet
, isEmpty
is false
since there are still elements left, and size
reflects the number of elements in the HashSet
.
HashSet
excels over arrays or ArrayList
s when working with unique elements — because of its ability to ward off duplicates. However, it's unsuitable for situations that require index-based access. HashSet
only holds non-primitive types, unlike arrays, which support both. Deciding when to use HashSet
, ArrayList
, or arrays is based on your specific requirements.
In this lesson, we explored HashSet
in Java, practiced adding and removing elements, and examined how to use HashSet
in comparison to Arrays and ArrayList
. Soon, you'll encounter practice exercises to solidify your understanding of HashSet
, which will further enhance your problem-solving skills. Keep up the pace - you're doing great on your journey to mastering Java!