DEV Community

Gabriel Babler
Gabriel Babler

Posted on

ArrayList vs HashSet vs HashMap - The main differences

Hello, folks! Today I want to share with you the main differences between those three implementations of Collection from Java - ArrayList, HashSet, and HashMap.

I chose those 3 because with them we can build a lot of solutions depending on the problem and, you can use them to help you through your logical challenges (HackerRank, LeetCode, etc).

Note that, we have more implementations, so don't be stuck just with those three of this post. =)

The objective of this post is to show you some differences between each implementation, and with that, you can choose the best one to solve your problem.

First, what can we understand as a Collection?

A collection is a set of data of the same type.
Example:

[
    "Gabriel",
    "John",
    "Alexa",
    "Bob"
]
Enter fullscreen mode Exit fullscreen mode

In this example, we can see a collection of names (or Strings).

We can also have a collection of objects, for example:

[
    {
        "name":"Gabriel",
        "age":26
    },
    {
        "name":"John"
        "age":30
    },
    {
        "name":"Alexa"
        "age":10
    },
    {
        "name":"Bob"
        "age":5
    }
]
Enter fullscreen mode Exit fullscreen mode

ArrayList

ArrayList is an implementation from the List interface.

The ArrayList implementation is probably the first one you will have contact with when you start learning Java.

So, when we are trying to create a list, like those examples of Collection above, in Java, we can use the ArrayList implementation to build it.

List<String> names = new ArrayList<>();
List<UserObject> users = new ArrayList<>();

What does using the ArrayList implementation give us?

There are some methods implemented already in ArrayList, like:

  • add(E e);
    (E is a generic type, I will create another post to talk more about it, basically, it means it can receive any Type as an argument - but of course, it has to respect the type of your List.)

  • get(int index);
    (index is the position where your data is present - remember that in a collection the index starts at 0)

  • remove(int index);

  • isEmpty();

  • size();

and more here.

Using ArrayList you can have a resizable list, removing or adding as much data as you want.

HashSet

HashSet is an implementation from the Set interface.

This implementation helps us to guarantee that our collection won't have repeated values, BUT it does not ensure sort.

So, if we have a list called names, like:

[
    "Gabriel",
    "John",
    "Gabriel",
    "Bob"
]
Enter fullscreen mode Exit fullscreen mode

If we want to remove all the repeated values, we could create a new instance of HashSet and pass the list names in the constructor, for example:

Set<String> uniqueNames = new HashSet<>(names);

Just like the ArrayList, the HashSet implementation has some methods implemented as well:

  • contains(Object o);

  • add(E e);
    If you try to add an existing value it will just ignore it and not repeat it.

  • isEmpty();

  • remove(Object o);

  • size();

and more here.

HashMap

HashMap is an implementation from the Map interface.

HashMap works a little bit differently than HashSet and ArrayList, because it receives two parameters - the KEY type and the VALUE type.

To instantiate a new HashMap we do this:

Map<Integer, String> users = new HashMap<>();

My first argument inside the <> is the type of my key - in this case an Integer. And the second one is the type of my value - in this case, a String.
Note that we don't need to have the same types in both places.

When we are working with HashMap, our KEY value is UNIQUE, so it won't be duplicated. But the value could be.

Just like the previous ones, the HashMap implementation has some methods implemented as well:

  • put(K key, V value);
    (Again, K and V here are generics and we are going to talk more about them in another post.)

  • get(Object key);

  • getOrDefault(Object key, V defaultValue);

  • isEmpty();

  • size();

  • containsValue(Object value);

  • containsKey(Object key);

  • putIfAbsent(K key, V value)

and more here.

Conclusion

I hope with this post now you can make use of those three collections to solve your daily problems.
An excellent way to practice them is through HackerRank, LeetCode, or any other coding challenges platform.

I hope I clarified the main differences between them and when you can choose one or another.

Please, feel free to reach me if you have any suggestions, questions, or anything else.

That's all folks! See you in the next post! =)

Top comments (0)