0
0
JavaHow-ToBeginner · 2 min read

Java How to Convert HashMap to List with Examples

To convert a HashMap to a List in Java, use new ArrayList<>(hashMap.keySet()) for keys, new ArrayList<>(hashMap.values()) for values, or new ArrayList<>(hashMap.entrySet()) for key-value pairs.
📋

Examples

Input{"a":1, "b":2}
OutputKeys list: [a, b], Values list: [1, 2], Entries list: [a=1, b=2]
Input{}
OutputKeys list: [], Values list: [], Entries list: []
Input{"x":10}
OutputKeys list: [x], Values list: [10], Entries list: [x=10]
🧠

How to Think About It

Think about what part of the HashMap you want to convert: keys, values, or entries. Each part can be collected into a List by taking the set or collection from the map and creating a new ArrayList from it.
📐

Algorithm

1
Get the HashMap input.
2
Choose whether to convert keys, values, or entries.
3
Use the appropriate method: keySet(), values(), or entrySet() to get a collection.
4
Create a new ArrayList from that collection.
5
Return or use the new List.
💻

Code

java
import java.util.*;

public class HashMapToList {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("a", 1);
        map.put("b", 2);

        List<String> keys = new ArrayList<>(map.keySet());
        List<Integer> values = new ArrayList<>(map.values());
        List<Map.Entry<String, Integer>> entries = new ArrayList<>(map.entrySet());

        System.out.println("Keys list: " + keys);
        System.out.println("Values list: " + values);
        System.out.println("Entries list: " + entries);
    }
}
Output
Keys list: [a, b] Values list: [1, 2] Entries list: [a=1, b=2]
🔍

Dry Run

Let's trace converting keys from the map {"a"=1, "b"=2} to a list.

1

Start with HashMap

map = {"a"=1, "b"=2}

2

Get keys set

map.keySet() = ["a", "b"]

3

Create ArrayList from keys

new ArrayList<>(["a", "b"]) = ["a", "b"]

StepActionResult
1HashMap content{"a"=1, "b"=2}
2Get keys set["a", "b"]
3Create list from keys["a", "b"]
💡

Why This Works

Step 1: Extract keys, values, or entries

The HashMap provides keySet(), values(), and entrySet() methods to get collections of keys, values, or key-value pairs.

Step 2: Create a List from the collection

Wrap the collection in a new ArrayList to convert it into a List, which supports ordered access and list operations.

Step 3: Use the List as needed

The resulting List can be used like any other list in Java, such as iterating or accessing elements by index.

🔄

Alternative Approaches

Stream API
java
import java.util.*;
import java.util.stream.*;

public class HashMapToListStream {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("a", 1);
        map.put("b", 2);

        List<String> keys = map.keySet().stream().collect(Collectors.toList());
        List<Integer> values = map.values().stream().collect(Collectors.toList());
        List<Map.Entry<String, Integer>> entries = map.entrySet().stream().collect(Collectors.toList());

        System.out.println("Keys list: " + keys);
        System.out.println("Values list: " + values);
        System.out.println("Entries list: " + entries);
    }
}
Using streams is more flexible and fits functional programming style but is slightly more verbose.
Manual iteration
java
import java.util.*;

public class HashMapToListManual {
    public static void main(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("a", 1);
        map.put("b", 2);

        List<String> keys = new ArrayList<>();
        for (String key : map.keySet()) {
            keys.add(key);
        }

        System.out.println("Keys list: " + keys);
    }
}
Manual iteration is clear and explicit but more code and less concise.

Complexity: O(n) time, O(n) space

Time Complexity

Creating a list from the HashMap collections requires iterating over all elements once, so it takes O(n) time where n is the number of entries.

Space Complexity

The new list stores all elements, so it uses O(n) extra space proportional to the number of entries.

Which Approach is Fastest?

Using the constructor of ArrayList with the collection is fastest and simplest; streams add overhead but offer flexibility; manual iteration is slower and more verbose.

ApproachTimeSpaceBest For
ArrayList constructorO(n)O(n)Simple and fast conversion
Stream APIO(n)O(n)Functional style and chaining
Manual iterationO(n)O(n)Explicit control, educational
💡
Use new ArrayList<>(hashMap.keySet()) to quickly get a list of keys.
⚠️
Trying to cast the keySet or values collection directly to a List without creating a new ArrayList causes a ClassCastException.