Difference between ArrayList and HashSet

Difference between ArrayList and HashSet

1. Internal Data Structure

  • ArrayList: ArrayList uses an array internally to store elements. It maintains the insertion order of elements, allowing duplicate elements, and provides constant-time access to elements by index. However, inserting or removing elements in the middle of an ArrayList can be less efficient since it requires shifting subsequent elements.
  • HashSet: HashSet uses a hash table internally to store elements. It does not maintain the insertion order of elements and does not allow duplicate elements. HashSet provides constant-time performance for basic operations like add, remove, and contains, making it efficient for set operations.

2. Duplication

  • ArrayList: ArrayList allows duplicate elements, meaning you can store the same element multiple times in the list.
  • HashSet: HashSet does not allow duplicate elements. If you attempt to add a duplicate element to a HashSet, it will simply ignore the duplicate and retain only unique elements.

3. Ordering

  • ArrayList: ArrayList maintains the order in which elements are inserted into the list. When you iterate over an ArrayList, you’ll encounter elements in the same order in which they were added.
  • HashSet: HashSet does not maintain any specific order of elements. When you iterate over a HashSet, the order of elements is not guaranteed and may change over time, as it depends on the internal hashing mechanism.

4. Performance

  • ArrayList: ArrayList provides constant-time access to elements by index but may have slower performance for operations like add or remove in the middle of the list, especially for large lists, due to the need to shift elements.
  • HashSet: HashSet provides constant-time performance for basic operations like add, remove, and contains, making it efficient for set operations. However, the performance of HashSet might degrade if the hash function is poorly designed or if there are many hash collisions.

ArrayList and HashSet

Now, let’s provide Java code examples to illustrate the differences between ArrayList and HashSet:

java

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CollectionComparison {
    public static void main(String[] args) {
        // ArrayList example
        List<String> arrayList = new ArrayList<>();
        arrayList.add("Apple");
        arrayList.add("Banana");
        arrayList.add("Orange");
        arrayList.add("Apple"); // Duplicate element
        
        // HashSet example
        Set<String> hashSet = new HashSet<>();
        hashSet.add("Apple");
        hashSet.add("Banana");
        hashSet.add("Orange");
        hashSet.add("Apple"); // Duplicate element, will be ignored

        // Output ArrayList elements
        System.out.println("ArrayList elements:");
        for (String fruit : arrayList) {
            System.out.println(fruit);
        }

        // Output HashSet elements
        System.out.println("HashSet elements:");
        for (String fruit : hashSet) {
            System.out.println(fruit);
        }
    }
}

ArrayList and HashSet example

In this example, we create an ArrayList and a HashSet, and add some elements to both collections. We include a duplicate element (“Apple”) in both collections to demonstrate how they handle duplication. When we iterate over the elements of each collection, we can observe the differences in ordering and duplication handling between ArrayList and HashSet.

Homepage

Readmore