Open In App

Convert ArrayList to HashMap in Java

Last Updated : 28 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Array List can be converted into HashMap, but the HashMap does not maintain the order of ArrayList. To maintain the order, we can use LinkedHashMap which is the implementation of HashMap.

Basically, there are two different ways to convert ArrayList to Hashmap-

  1. Using ArrayList Iteration
  2. Using ArrayList Iteration with LinkedHashMap

Using ArrayList Iteration:

Here, we just need to iterate on each of the elements of the ArrayList and the element can be converted into the key-value pair and store in the HashMap. 

Java




// Java program to convert ArrayList
// to HashMap
  
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
  
public class ArrayListExample {
    public static void main(String[] args)
    {
         
        // ArrayList of string
        ArrayList<String> languageList
           = new ArrayList<>(Arrays.asList("Java", "C++", "Python"
                                           "PHP", "NodeJS"));
  
        System.out.println(
            "-------------ArrayList---------------");
       
        
        // printing the ArrayList
        for (String language : languageList)
        {
           System.out.println(language);
        }
  
        System.out.println(
            "--------------HashMap----------------");
         
        // convertArrayListToHashMap() method directly 
        // converts ArrayList to Hashmap
        HashMap<String, Integer> languageMap = convertArrayListToHashMap(languageList);
    
        // printing the HashMap
        for (Map.Entry<String, Integer> entry : languageMap.entrySet()) {
  
            System.out.println(entry.getKey() + " : "
                               + entry.getValue());
        }
    }
  
    private static HashMap<String, Integer>
    convertArrayListToHashMap(ArrayList<String> arrayList)
    {
  
        HashMap<String, Integer> hashMap = new HashMap<>();
  
        for (String str : arrayList) {
  
            hashMap.put(str, str.length());
        }
  
        return hashMap;
    }
}


Output

-------------ArrayList---------------
Java
C++
Python
PHP
NodeJS
--------------HashMap----------------
Java : 4
C++ : 3
PHP : 3
NodeJS : 6
Python : 6

Using ArrayList Iteration with LinkedHashMap :

  • Here, the  Array List is converted into a HashMap but HashMap does not maintain the order of the ArrayList.
  • To maintain the order, we use LinkedHashMap which is an implementation of HashMap and helps us to maintain the order of the elements, and we can easily convert Arraylist to Hashmap.

Java




// Java program to convert ArrayList
// to HashMap
  
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.*;
  
public class ArrayListExample {
    public static void main(String[] args)
    {
        // ArrayList of string
        ArrayList<String> languageList
            = new ArrayList<>(Arrays.asList(
                "Java", "C++", "Python", "PHP", "NodeJS"));
  
        System.out.println(
            "-------------ArrayList---------------");
          
        // printing the ArrayList
        for (String language : languageList) {
  
            System.out.println(language);
        }
  
        System.out.println(
            "--------------HashMap----------------");
         
        // convertArrayListToHashMap() method directly 
        // converts ArrayList to HashMap
        HashMap<String, Integer> languageMap
            = convertArrayListToHashMap(languageList);
  
        
        // printing the HashMap
        for (Map.Entry<String, Integer> entry :
             languageMap.entrySet()) {
  
            System.out.println(entry.getKey() + " : "
                               + entry.getValue());
        }
    }
  
    private static HashMap<String, Integer>
                       convertArrayListToHashMap(ArrayList<String> arrayList)
    {
  
        LinkedHashMap<String, Integer> linkedHashMap
                                  = new LinkedHashMap<>();
  
        for (String str : arrayList) {
  
            linkedHashMap.put(str, str.length());
        }
  
        return linkedHashMap;
    }
}


Output

-------------ArrayList---------------
Java
C++
Python
PHP
NodeJS
--------------HashMap----------------
Java : 4
C++ : 3
Python : 6
PHP : 3
NodeJS : 6


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads