Difference between HashMap and ArrayList

Difference between HashMap and ArrayList

1. Data Structure

  • HashMap: HashMap is a collection that stores key-value pairs. It uses a hash table data structure internally to store elements. Each key in a HashMap must be unique, and it is associated with exactly one value. HashMap allows null keys and null values, but only one null key. HashMap provides constant-time performance for basic operations like put, get, and remove, assuming a good hash function and proper load factor.
  • ArrayList: ArrayList is a dynamic array that stores elements in a sequential order. It allows duplicate elements and maintains the insertion order of elements. ArrayList provides constant-time access to elements by index, making it efficient for random access.

2. Usage

  • HashMap: HashMap is used when you need to store and retrieve values based on some unique identifier or key. It is suitable for scenarios where you want to associate each element with a specific key for easy lookup and retrieval.
  • ArrayList: ArrayList is used when you need to store a collection of elements in a sequential order. It is suitable for scenarios where you need to maintain the order of elements and frequently access them by their index.

3. Performance

  • HashMap: HashMap provides constant-time performance for basic operations like put, get, and remove, assuming a good hash function and proper load factor. However, the actual performance may degrade if there are many hash collisions or if the hash function is poorly designed.
  • ArrayList : ArrayList provides constant-time performance for accessing elements by index but may have slower performance for operations like adding or removing elements in the middle of the list, especially for large lists, due to the need to shift elements.

4. Memory Usage

  • HashMap: HashMap typically consumes more memory compared to ArrayList due to the overhead of storing key-value pairs and maintaining the internal hash table data structure.
  • ArrayList: ArrayList generally consumes less memory compared to HashMap since it only needs to store the elements themselves in a sequential order.

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

HashMap and ArrayList

Example
java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CollectionComparison {
    public static void main(String[] args) {
        // HashMap example
        Map<String, Integer> hashMap = new HashMap<>();
        hashMap.put("One", 1);
        hashMap.put("Two", 2);
        hashMap.put("Three", 3);
        
        // ArrayList example
        List<String> arrayList = new ArrayList<>();
        arrayList.add("Apple");
        arrayList.add("Banana");
        arrayList.add("Orange");

        // Accessing HashMap elements
        System.out.println("HashMap elements:");
        for (Map.Entry<String, Integer> entry : hashMap.entrySet()) {
            System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
        }

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

HashMap and ArrayList Example

In this example, we create a HashMap and an ArrayList, and add some elements to both collections. We then iterate over the elements of each collection to demonstrate the differences in usage and internal data structure between HashMap and ArrayList.

Homepage

Readmore