Difference between ArrayList and HashSet in Java? Answer with Example (original) (raw)

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 ArrayListand 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 ArrayListand 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 ArrayList allows 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 ArrayListis 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 ArrayListis backed by an Array while HashSetis 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.

ArrayList and HashSet Example in Java

Here's an example that demonstrates the difference between ArrayList and HashSet:

import java.util.ArrayList;

import java.util.HashSet;

import java.util.Iterator;

public class ArrayListVsHashSetExample {

public static void main(String[] args) {

// Example using ArrayList

ArrayList arrayList = new ArrayList<>();

// Adding elements to ArrayList

arrayList.add("Apple");

arrayList.add("Banana");

arrayList.add("Orange");

arrayList.add("Apple"); // Duplicate element

// Displaying elements of ArrayList

System.out.println("ArrayList Elements:");

for (String fruit : arrayList) {

System.out.println(fruit);

}

// Example using HashSet

HashSet hashSet = new HashSet<>();

// Adding elements to HashSet

hashSet.add("Apple");

hashSet.add("Banana");

hashSet.add("Orange");

hashSet.add("Apple"); // Duplicate element (ignored in a HashSet)

// Displaying elements of HashSet using Iterator

System.out.println("\nHashSet Elements:");

Iterator iterator = hashSet.iterator();

while (iterator.hasNext()) {

System.out.println(iterator.next());

}

}

}

In the example, you can observe that the ArrayList allows the addition of duplicate elements, while the HashSet automatically ignores duplicates. The order of elements in the ArrayList is maintained, whereas the HashSet does not guarantee any specific order when iterating over elements.

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

Difference between ArrayList and HashSet in Java