Difference between ArrayList and HashSet in Java

ArrayList vs HashSet Java
Main difference between ArrayList and HashSet is that one is a List implementation while other is a Set implementation. It means all the differences between a List data structure and a Set data structure also applies to this pair. For example, List implementations are ordered, it store element in the order they were added, while Set implementation doesn't provide such guarantee. Similarly, since List provides Random access, you can access any element directly if you know the index, but Set doesn't provide such facility. You need to Iterate through whole collection to get access of any elements. We will see couple of more difference in this Java tutorial. By the way ArrayList and HashSet are two most common Collection class used in Java programming language and before discussing 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:

Difference between ArrayList and HashSet in Java J2EE1) Both ArrayList and HashSet are non synchronized collection class and not meant to be used in multi-threading and concurrent environment. You can  make ArrayList and HashSet synchronized by using Collections.synchroinzedCollection() just like we make ArrayList and HashSet read only other day.

2) Both ArrayList and HashSet can be traversed using Iterator. This is in fact a preferred way if you want to perform operation 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 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 fist difference and property of implementing List and Set interface.

3) Third difference 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) Fourth difference between ArrayList and HashSet is that ArrayList is backed by an Array while HashSet is backed by an HashMap instance. See how HashSet internally works in Java for more details.

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


That's all on difference between ArrayList and HashSet. these differences helps 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. raw array is fasted among them.

5 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