Difference between ArrayList and HashSet in Java? Answer

ArrayList vs HashSet Java
The main difference between ArrayList and HashSet is that one is a List implementation while the other is a Set implementation. It means all the differences between a List data structure and a Set data structure also apply to this pair. For example, List implementations are ordered, it stores the element in the order they were added, while Set implementation doesn't provide such a guarantee. Similarly, since List provides Random access, you can access any element directly if you know the index, but Set doesn't provide such a facility. 

You need to Iterate through the whole collection to get access to any elements. We will see a couple of more differences in this Java tutorial. 

By the way ArrayList and HashSet are the two most common Collection classes used in Java programming language and before discussing the difference between ArrayList vs HashSet, let's see some similarities between them :



Similarities ArrayList and HashSet

Here are couple of similarities between ArrayList and HashSet in Java:

1) Both ArrayList and HashSet are non synchronized collection classes and not meant to be used in multi-threading and concurrent environments. You can make ArrayList and HashSet synchronized by using Collections.synchroinzedCollection() just like we make ArrayList and HashSet read-only other days.

2) Both ArrayList and HashSet can be traversed using Iterator. This is in fact a preferred way if you want to perform operations on all elements.

3) Iterator of ArrayList and HashSet both are fail-fast, i.e. they will throw ConcurrentModificationException if ArrayList or HashSet is modified structurally once Iterator has been created.

Now let's see some differences between ArrayList and HashSet in Java 





Difference between ArrayList vs HashSet in Java

Here are a couple of differences between ArrayList and HashSet in Java:

1. First and most important difference between ArrayList and HashSet is that ArrayList implements List interface while HashSet implements Set interface in Java.

2. Another difference between ArrayList and HashSet is that ArrayListallow duplicates while HashSet doesn't allow duplicates. This is the side effect of first difference and property of implementing List and Set interface.

3. The differences between ArrayList and HashSet is that ArrayList is an ordered collection and maintains insertion order of elements while HashSet is an unordered collection and doesn't maintain any order.

4. The difference between ArrayList and HashSet is that ArrayList is backed by an Array while HashSet is backed by a HashMap instance. See how HashSet internally works in Java for more details.

5. Fifth difference between HashSet and ArrayList is that it's index-based you can retrieve objects by calling get(index) or remove objects by calling remove(index) while HashSet is completely object-based. HashSet also doesn't provide the get() method.

Here is a nice summary of the differences between ArrayList and HashSet in Java:

Difference between ArrayList and HashSet in Java


That's all on the difference between ArrayList and HashSet. these differences help you to decide where to use ArrayList and where to use HashSet in Java. in terms of performance between ArrayList and HashSet, choose what suits best to you. the raw array has fasted among them.

Further Learning
Difference between TreeSet and HashSet in Java
Difference between HashMap and ArrayList in Java

9 comments:

  1. Really useful thanks

    ReplyDelete
  2. Another difference is that ArrayList allows to add null value where as HashSet will throw exception if null is added.

    ReplyDelete
    Replies
    1. if you add null to HashSet we will not get any Exception also you can add multiple null values only one null will be added as HashSet maintains the uniqueness
      HashSet hs= new HashSet();
      hs.add("Laxmi");
      hs.add(null);
      hs.add(null);
      O/p
      [null, Laxmi]

      Delete
  3. A major advantage of HashSet over ArrayList is that lookup via the contains method is O(1) vs O(n).
    So for moderate to large data a HashSet contains will vastly outperform the ArrayList contains.

    ReplyDelete
  4. If we don't have duplicate data then which one is better to use among ArrayList and HashSet? And why?

    ReplyDelete
    Replies
    1. just use HashSet if you dont wanna have any duplicated data

      Delete

Feel free to comment, ask questions if you have any doubt.