HashSets – Java Collections Framework

Hello guys, I am back with our 4th article in my article series regarding the Java Collections Framework. Today I am going to educate you guys about the HashSet class in Java. Let’s get into the article.

Characteristics

HashSet class implements the Set interface in Java. The main thing you need to know about HashSets is that they do not guarantee to keep a certain order. This is one of the reasons why it only has a limited number of methods.

HashSet class is not synchronized. We can add null elements to HashSet. Also, there is a special functionality in HashSets. We can’t add duplicate values to HashSets. If we add a duplicate value, it will automatically overwrite the existing value. The same thing applies even if we try to add a null value.

Let me demonstrate some of the characteristics with some examples.

Does not maintain any order
        //Creating a HashSet
        HashSet myHashSet = new HashSet();

        //Adding Elements
        myHashSet.add("Joey");
        myHashSet.add("Chandler");
        myHashSet.add("Ross");
        myHashSet.add("Monica");
        System.out.println(myHashSet);

        myHashSet.add("Rachel");
        myHashSet.add("Pheobe");
        myHashSet.add(null);
        System.out.println(myHashSet);

        myHashSet.add("Gunther");
        System.out.println(myHashSet);

Here is the console output for the above code snippet.

[Joey, Ross, Chandler, Monica]
[null, Rachel, Pheobe, Joey, Ross, Chandler, Monica]
[null, Gunther, Rachel, Pheobe, Joey, Ross, Chandler, Monica]

As you can see, the null value has come to the beginning of the list, even though we did add elements after adding the null value.

Can’t Add Duplicates
        //Creating a HashSet
        HashSet<String> myHashSet = new HashSet<>();

        //Adding Elements
        myHashSet.add(null);
        myHashSet.add("Joey");
        myHashSet.add("Chandler");
        myHashSet.add("Ross");
        myHashSet.add("Monica");
        myHashSet.add("Monica");
        System.out.println(myHashSet);

        myHashSet.add(null);
        System.out.println(myHashSet);

Here is the console output for this example.

[null, Joey, Ross, Chandler, Monica]
[null, Joey, Ross, Chandler, Monica]

You can clearly see even though we add duplicate elements in the code, there are no duplicates in the list.

Methods

1. boolean add (Element e)

We can add elements to the HashSet using this method. The method will return true if the process was successful. So, if we are trying to add a duplicate element, this will return false since the process was not successful.

2. void clear()

We can clear the whole HashSet using this method.

3. boolean contains (Object obj)

This returns true if the specified object is there in the HashSet.

4. boolean isEmpty()

This returns true if the HashSet is empty

5. boolean remove(Object obj)

If the specified object is present in the list, this remove that element from the HashSet and returns true. If the specified object is not there in the list, this returns false.

6. int size()

This returns the number of elements in the set

You can always refer to Java Documentation for more details.


Alright coders, this ends my article about HashSet class in Java. If you learned something new, drop a like and follow my blog. Stay Safe ✌

Published by Thisura Thenuka

I am a passionate software engineering student. But cricket is my first love ❤

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: