Open In App

How to Convert HashMap to ArrayList in Java?

Last Updated : 12 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java a HashMap is a collection that stores key-value pairs on the other hand, an ArrayList is a collection that stores dynamic arrays. There are some scenarios where we need to convert a HashMap into an ArrayList such as:

  • Extracting only the keys or values in the list form.
  • Converting key-value pairs into a list of objects for further processing.

Methods to Convert HashMap to an ArrayList

1. Converting HashMap keys to an ArrayList

If we need an ArrayList containing only the keys from the HashMap, we can use the keySet() method in the HashMap and convert it to an ArrayList.

Example: This example demonstrates how to extract the keys of a HashMap and store them in an ArrayList.

Java
// Java Program to demonstrates 
// the working of keySet()
import java.util.*;

public class Geeks {
    public static void main(String[] args) {
        HashMap<String, Integer> m = new HashMap<>();
        m.put("Geek1", 100);
        m.put("Geek2", 200);
        m.put("Geek3", 300);

        ArrayList<String> al = new ArrayList<>(m.keySet());
        System.out.println("Keys: " + al);
    }
}

Output
Keys: [Geek3, Geek2, Geek1]

2. Converting HashMap values to an ArrayList

If we need an ArrayList containing only the values from the HashMap, we can use the values() method in the HashMap and convert it to an ArrayList.

Example: This example demonstrates how to extract the values of a HashMap and store them in an ArrayList.

Java
// Java Prpgram to demonstrates the working of values()
import java.util.*;

public class Geeks {
    public static void main(String[] args) {
        HashMap<String, Integer> m = new HashMap<>();
        m.put("Geek1", 100);
        m.put("Geek2", 200);
        m.put("Geek3", 300);

        ArrayList<Integer> al = new ArrayList<>(m.values());
        System.out.println("Values: " + al);
    }
}

Output
Values: [300, 200, 100]

3. Converting HashMap key-value pairs to an ArrayList

If we want to keep a relationship between each key paired with its value, we can use the entrySet() method, this method returns a set of key value pairs and we can convert it into an ArrayList to work with it like a list.

Example: This example demonstrates how to convert the key-value pairs of a HashMap into an ArrayList of Map.Entry Objects.

Java
// Java Program to demonstrates 
// the working of entrySet()
import java.util.*;

public class Geeks {
    public static void main(String[] args) {
        HashMap<String, Integer> m = new HashMap<>();
        m.put("Geek1", 100);
        m.put("Geek2", 200);
        m.put("Geek3", 300);

        // Convert HashMap key-value pairs into an ArrayList
        ArrayList<Map.Entry<String, Integer>> al = new ArrayList<>(m.entrySet());

        // Print the ArrayList containing key-value pairs
        System.out.println("Key-Value Pairs: " + al);
    }
}

Output
Key-Value Pairs: [Geek3=300, Geek2=200, Geek1=100]


Next Article

Similar Reads