Java How to Convert HashMap to List with Examples
new ArrayList<>(hashMap.keySet()) for keys, new ArrayList<>(hashMap.values()) for values, or new ArrayList<>(hashMap.entrySet()) for key-value pairs.Examples
How to Think About It
Algorithm
Code
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); } }
Dry Run
Let's trace converting keys from the map {"a"=1, "b"=2} to a list.
Start with HashMap
map = {"a"=1, "b"=2}
Get keys set
map.keySet() = ["a", "b"]
Create ArrayList from keys
new ArrayList<>(["a", "b"]) = ["a", "b"]
| Step | Action | Result |
|---|---|---|
| 1 | HashMap content | {"a"=1, "b"=2} |
| 2 | Get keys set | ["a", "b"] |
| 3 | Create 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
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); } }
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); } }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| ArrayList constructor | O(n) | O(n) | Simple and fast conversion |
| Stream API | O(n) | O(n) | Functional style and chaining |
| Manual iteration | O(n) | O(n) | Explicit control, educational |
new ArrayList<>(hashMap.keySet()) to quickly get a list of keys.