Skip to content

Java HashSet

First things first, what exactly is a HashSet, you ask? Well, think of it as a special kind of collection in Java that stores unique elements. It's like having a magical bag where you can keep your treasures, but each treasure must be unique—no duplicates allowed! Plus, HashSet ensures lightning-fast access to your elements, making it perfect for tasks where you need quick lookups and uniqueness.

Now, let's dig deeper into the workings of HashSet and discover its wonders:

Creating a HashSet

To create a HashSet in Java, it's as easy as waving a wand! You simply declare a new HashSet object, like so:

java
public class MyHashSet{
    public static void main(String[] args) {
        HashSet<String> mySet = new HashSet<>();
    }
}

Here, String represents the type of elements our HashSet will hold. It could be any type you want—integers, objects, or even custom classes!

Adding Elements

Now, let's sprinkle some magic into our HashSet by adding some elements:

java
public class MyHashSet{
    public static void main(String[] args) {
        HashSet<String> mySet = new HashSet<>();
        mySet.add("apple");
        mySet.add("banana");
        mySet.add("orange");
    }
}

Voila! Our HashSet now contains three delicious fruits—apple, banana, and orange. And guess what? They're all unique, just like magic!

Checking for Existence

One of the coolest things about HashSet is how quickly it can tell you whether an element exists within it. It's like having a crystal ball that can instantly answer your questions:

java
public class MyHashSet{
    public static void main(String[] args) {
        HashSet<String> mySet = new HashSet<>();
        mySet.add("apple");
        mySet.add("banana");
        mySet.add("orange");
        boolean hasApple = mySet.contains("apple");
        System.out.println("Does mySet contain an apple? " + hasApple); // Output: Does mySet contain an apple? true
    }
}

See? It's like magic! In the blink of an eye, HashSet tells us whether "apple" is among our treasures.

Removing Elements

But what if we decide we don't want bananas in our magical bag anymore? No problem! HashSet makes it a breeze to remove elements:

java
public class MyHashSet{
    public static void main(String[] args) {
        HashSet<String> mySet = new HashSet<>();
        mySet.add("apple");
        mySet.add("banana");
        mySet.add("orange");
        mySet.remove("banana");
        System.out.println("After removing banana: " + mySet); // Output: After removing banana: [apple, orange]
    }
}

And just like that, the banana disappears from our HashSet, leaving only the apple and orange behind.

Efficiency

One of the best qualities of HashSet is its lightning-fast performance. Thanks to its underlying data structure— a hash table—HashSet offers constant-time performance for basic operations like adding, removing, and checking for existence. It's like having a magical assistant who works tirelessly behind the scenes to ensure your HashSet stays efficient, no matter how many elements you throw into it!

Waytojava is designed to make learning easier. We simplify examples for better understanding. We regularly check tutorials, references, and examples to correct errors, but it's important to remember that humans can make mistakes.