In this post, we will see how to convert HashMap to ArrayList in java. Many times, you need to store keys or values into ArrayList or Store HashMap’s Node objects in ArrayList.
Here are the ways to convert HashMap to ArrayList in java.
Using Java 8’s Stream API
Java 8: Convert HashMap’s keys to ArrayList
We can use HashMap’s keyset()Â method to get a set of keys and use Java 8‘s Stream API to convert it to ArrayList.
1 2 3 4 5 |
List<Integer> customerIdList = customerIdNameMap.keySet() .stream() .collect(Collectors.toList()); |
Convert HashMap’s values to ArrayList
We can use HashMap’s values()
method to get a collection of values and use Java 8’s Stream API to convert it to ArrayList.
1 2 3 4 5 |
List customerNames = customerIdNameMap.values() .stream() .collect(Collectors.toList()); |
Convert HashMap’s Entry objects to ArrayList
We can use HashMap’s entrySet()
method to get set of Entry object and use Java 8’s Stream API to convert it to ArrayList.
1 2 3 4 5 |
List<Entry<Integer, String>> entryCustomerList = customerIdNameMap.entrySet() .stream() .collect(Collectors.toList()); |
Let’s see the complete example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.stream.Collectors; public class HashMapToArrayListMainJava8 { public static void main(String[] args) { HashMap<Integer, String> customerIdNameMap = new HashMap<Integer, String>(); // Putting key-values pairs in HashMap customerIdNameMap.put(1001, "Arman"); customerIdNameMap.put(1002, "Javin"); customerIdNameMap.put(1003, "Mat"); customerIdNameMap.put(1004, "Joe"); // Java 8 // Convert keys to ArrayList List<Integer> customerIdList = customerIdNameMap.keySet() .stream() .collect(Collectors.toList()); System.out.println("customerIds: "+customerIdList); // Convert values to ArrayList List<String> customerNames = customerIdNameMap.values() .stream() .collect(Collectors.toList()); System.out.println("Customer Names: "+ customerNames); // Convert entry objects to ArrayList List<Entry<Integer, String>> entryCustomerList = customerIdNameMap.entrySet() .stream() .collect(Collectors.toList()); System.out.println("Customer ID and Names: "+entryCustomerList); } } |
Output:
Using ArrayList’s constructor
Convert HashMap’s keys to ArrayList
Get
keySet()Â from HashMap and then convert it to ArrayList using its constructor.
1 2 3 4 5 |
// Convert keys to ArrayList Set keySet = customerIdNameMap.keySet(); List customerIdList = new ArrayList(keySet); |
Convert HashMap’s values to ArrayList
Get
values()Â from HashMap and then convert it to ArrayList using its constructor.
1 2 3 4 5 |
// Convert values to ArrayList Collection values = customerIdNameMap.values(); List customerNames = new ArrayList(values); |
Convert HashMap’s Entry objects to ArrayList
Get
entrySet()Â from HashMap and then convert it to ArrayList using its constructor.
1 2 3 4 5 |
// Convert entry objects to ArrayList Set<Entry<Integer, String>> entrySet = customerIdNameMap.entrySet(); List<Map.Entry<Integer,String>> entryCustomerList = new ArrayList<Map.Entry<Integer,String>>(entrySet); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package org.arpit.java2blog; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class HashMapToArrayListMain { public static void main(String[] args) { HashMap<Integer, String> customerIdNameMap = new HashMap<Integer, String>(); // Putting key-values pairs in HashMap customerIdNameMap.put(1001, "Arman"); customerIdNameMap.put(1002, "Javin"); customerIdNameMap.put(1003, "Mat"); customerIdNameMap.put(1004, "Joe"); // Before Java 8 // Convert keys to ArrayList Set<Integer> keySet = customerIdNameMap.keySet(); List<Integer> customerIdList = new ArrayList<Integer>(keySet); System.out.println("customerIds: "+customerIdList); // Convert values to ArrayList Collection<String> values = customerIdNameMap.values(); List<String> customerNames = new ArrayList<String>(values); System.out.println("Customer Names: "+ customerNames); // Convert entry objects to ArrayList Set<Entry<Integer, String>> entrySet = customerIdNameMap.entrySet(); List<Map.Entry<Integer,String>> entryCustomerList = new ArrayList<Map.Entry<Integer,String>>(entrySet); System.out.println("Customer ID and Names: "+entryCustomerList); } } |
Java HashMap tutorial
- HashMap in java
- HashMap internal working
- hash and indexfor method in HashMap
- hashcode and equals in javasort
- HashMap by keys and values
- Difference between HashMap and HashSet
- Difference between HashMap and Hashtable
- How to iterate over HashMap