Difference between ArrayList and HashSet in Java

link 0

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:

1) 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.

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.


We have built the accelerating growth-oriented website for budding engineers and aspiring job holders of technology companies such as Google, Facebook, and Amazon
If you would like to study our free courses you can join us at

http://www.techcodebit.com. #techcodebit #google #microsoft #facebook #interview portal #jobplacements


Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar