How to convert a HashMap to an ArrayList

HashMap is a collection class, based on Maps, that stores key and value pairs; they are denoted as HashMap<Key, Value> or HashMap<K, V>.

ArrayList provides the dynamic arrays in Java. These arrays use an ​iterator to access the objects stored in the ArrayList.

svg viewer

Conversion of HashMap to ArrayList

A HashMap contains key-value pairs, there are three ways to convert a HashMap to an ArrayList:

  1. Converting the HashMap keys into an ArrayList.

  2. Converting the HashMap values into an ArrayList.

  3. Converting the HashMap key-value pairs into an ArrayList.

The implementation of these three methods is shown below:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
class HashMapToArrayListConversion {
public static void main(String args[])
{
// HashMap with Country as key and capital as value
HashMap<String,String> countryCapitalMap=
new HashMap<String,String>();
countryCapitalMap.put("USA","Washington");
countryCapitalMap.put("Bangladesh","Dhaka");
countryCapitalMap.put("France","Paris");
countryCapitalMap.put("England","London");
countryCapitalMap.put("Russia","Moscow");
System.out.println("-----------------------------");
// Creating ArrayList from Keys
ArrayList<String> keysArrayList=
new ArrayList<String>(countryCapitalMap.keySet());
System.out.println("Countries (Keys) are: ");
for (String country: keysArrayList) {
System.out.println(country);
}
System.out.println("-----------------------------");
//Creating ArrayList from Values
ArrayList<String> valuesArrayList=
new ArrayList<String>(countryCapitalMap.values());
System.out.println("Capital (Values) are: ");
for (String capital:valuesArrayList) {
System.out.println(capital);
}
System.out.println("-----------------------------");
//Creating ArrayList from Entry set
ArrayList <Entry <String,String>> entryArrayList=
new ArrayList<Entry<String,String>>(countryCapitalMap.entrySet());
for (Entry <String, String> entry:entryArrayList) {
System.out.println("Country: "+ entry.getKey()
+ " and Capital: "+entry.getValue());
}
System.out.println("-----------------------------");
}
}

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved