ArrayList vs. HashMap in Java

ArrayList is a part of collection framework and is present in java.util package. It provides us dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed.

HashMap is a part of Java’s collection since Java 1.2. It provides the basic implementation of Map interface of Java. It stores the data in (Key, Value) pairs. To access a value one must know its key. HashMap is known as HashMap because it uses a technique called Hashing. Hashing is a technique of converting a large String to small String that represents same String. A shorter value helps in indexing and faster searches. HashSet also uses HashMap internally. It internally uses a link list to store key-value pairs already explained in HashSet in detail and further articles.

java-arraylist




This article discusses the similarities and differences between the ArrayList and HashMap in Java.

Similarity between ArrayList and HashMap in Java

  • The ArrayList and HashMap, both are not synchronised. So in-order to use them in the multi-threading environment, it needs to be first synchronised.
  • Both ArrayList and HashMap allow null. ArrayList allow null Values and HashMap allows null key and values
  • Both ArrayList and HashMap allow duplicates, ArrayList allows duplicate elements and HashMap allow duplicate values
  • Both ArrayList and HashMap can be traversed through Iterator in Java.
  • Both Somewhere use array, ArrayList is backed by array and HashMap is also internally implemented by Array
  • Both are use get() method, ArrayList.get() method works based on index and HashMap.get() method take one parameter key_element of object type and refers to the key whose associated value is supposed to be fetched so both provides constant-time performance.

Difference between ArrayList and HashMap in Java

  1. Interface Implemented: ArrayList implement List Interface while HashMap is an implementation of Map interface.

    ArrayList Class Declaration:

    public class ArrayList 
        extends AbstractList 
        implements List, RandomAccess, Cloneable, Serializable
    

    HashMap Class Declaration:



    public class HashMap 
        extends AbstractMap 
        implements Map, Cloneable, Serializable  
    
  2. Maintenance of the Insertion Order: ArrayList maintains the insertion order while HashMap does not maintain insertion order. Which means ArrayList returns the list items in the same order while HashMap doesn’t maintain any order so returned key-values pairs any kind of order.

    Below example illustrates this difference:

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import java.util.*;
      
    class GFG {
      
        public static void main(String args[])
        {
            // Creating ArrayList
            ArrayList<String> list
                = new ArrayList<String>();
      
            // Adding object in ArrayList
            list.add("A");
            list.add("B");
            list.add("C");
            list.add("D");
      
            // Invoking ArrayList object
            System.out.println("ArrayList: " + list);
      
            // Creating HashMap
            HashMap<Integer, String> hm
                = new HashMap<Integer, String>();
      
            // Adding object in HashMap
            hm.put(1, "A");
            hm.put(2, "B");
            hm.put(3, "C");
            hm.put(4, "D");
      
            // Invoking HashMap object
            // It might or might not display elements
            // in the insertion order
            System.out.print("HasMap: " + hm);
        }
    }

    chevron_right

    
    

    Output:

    ArrayList: [A, B, C, D]
    HasMap: {1=A, 2=B, 3=C, 4=D}
    
  3. Memory Consumption: ArrayList stores the elements only as value and maintain internally the indexing for every element. While HashMap stores elements with key and value pair, i.e. two objects. So HashMap takes more memory comparatively.

    ArrayList:

    // String value is stored in ArrayList
    list.add("A");
    

    HashMap:

    // Two String values stored
    // as the key value pair in HashMap
    hm.put(1, "A");
    
  4. Duplicates: ArrayList allows duplicate elements while HashMap doesn’t allow duplicate keys but does allow duplicate values.

    Below example illustrates this difference:

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import java.util.*;
      
    class GFG {
      
        public static void main(String args[])
        {
            // Creating ArrayList
            ArrayList<String> list
                = new ArrayList<String>();
      
            // Adding object in ArrayList
            list.add("A");
            list.add("B");
      
            // Add duplicates
            list.add("A");
            list.add("A");
      
            // Invoking ArrayList object
            System.out.println("ArrayList: " + list);
      
            // Creating HashMap
            HashMap<Integer, String> hm
                = new HashMap<Integer, String>();
      
            // Adding object in HashMap
            hm.put(1, "A");
            hm.put(2, "B");
      
            // Add duplicates key
            // Change value if index exist
            hm.put(3, "A");
            hm.put(3, "A");
      
            // Add duplicates values
            // allow duplicates value
            hm.put(4, "A");
            hm.put(5, "A");
      
            // Invoking HashMap object
            System.out.print("HasMap: " + hm);
        }
    }

    chevron_right

    
    

    Output:



    ArrayList: [A, B, A, A]
    HasMap: {1=A, 2=B, 3=A, 4=A, 5=A}
    
  5. Easiness while fetching an element: In ArrayList, an element can be fetched easily by specifying the index of it. But in HashMap, the elements is fetched by its corresponding key. It means that the key must be remembered always.

    ArrayList get(index) method always gives O(1) time complexity While HashMap get(key) can be O(1) in the best case and O(n) in the worst case time complexity.

    Below example illustrates this difference:

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import java.util.*;
      
    class GFG {
      
        public static void main(String args[])
        {
            // Creating ArrayList
            ArrayList<String> list
                = new ArrayList<String>();
      
            // Adding object in ArrayList
            list.add("A");
            list.add("B");
            list.add("C");
            list.add("D");
      
            // Invoking ArrayList object
            System.out.println("First Element of ArrayList: "
                               + list.get(0));
            System.out.println("Third Element of ArrayList: "
                               + list.get(2));
      
            // Creating HashMap
            HashMap<Integer, String> hm
                = new HashMap<Integer, String>();
      
            // Adding object in HashMap
            hm.put(1, "A");
            hm.put(2, "B");
            hm.put(3, "C");
            hm.put(4, "D");
      
            // Invoking HashMap object
            System.out.println("HashMap value at Key 1: "
                               + hm.get(1));
            System.out.println("HashMap value at Key 3: "
                               + hm.get(3));
        }
    }

    chevron_right

    
    

    Output:

    First Element of ArrayList: A
    Third Element of ArrayList: C
    HashMap value at Key 1: A
    HashMap value at Key 3: C
    
  6. Storing nulls: In ArrayList, any number of null elements can be stored. While in HashMap, only one null key is allowed, but the values can be of any number.

    Below example illustrates this difference:

    Example:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    import java.util.*;
      
    class GFG {
      
        public static void main(String args[])
        {
            // Creating ArrayList
            ArrayList<String> list
                = new ArrayList<String>();
      
            // Adding object in ArrayList
            list.add("A");
      
            // add first null value
            list.add(null);
            list.add("C");
      
            // add two null value again
            list.add(null);
            list.add(null);
      
            // Invoking ArrayList object
            System.out.println("ArrayList: "
                               + list);
      
            // Creating HashMap
            HashMap<Integer, String> hm
                = new HashMap<Integer, String>();
      
            // Adding object in HashMap
            hm.put(1, "A");
            hm.put(2, "B");
      
            // add null key
            hm.put(null, "C");
      
            // add again null key
            // which replace value of first
            // insert null key value
            hm.put(null, null);
      
            // add second null value
            hm.put(3, null);
      
            // Printing the HashMap
            System.out.println("HasMap: " + hm);
        }
    }

    chevron_right

    
    

    Output:

    ArrayList: [A, null, C, null, null]
    HasMap: {null=null, 1=A, 2=B, 3=null}
    

Difference between ArrayList and HashMap

ArrayList HashMap
The java ArrayList implements List Interface The java HashMap is implements Map interface
ArrayList always maintain insertion order of the elements HashMap does not maintain the insertion order
ArrayList only stores value or element HashMap stores key and value pairs
ArrayList can contain duplicate elements HashMap does not contain duplicate keys but contain duplicate values.
We can have any numbers of null elements in ArrayList We can have only one null key and any number of null values in HashMap
ArrayList get() method always gives an O(1) performance HashMap get()method can be O(1) in the best case and O(n) in the worst case


My Personal Notes arrow_drop_up

Strategy Path planning and Destination matters in success No need to worry about in between temporary failures

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.