Two of the most used data structures in java are HashMap and ArrayList. Inherited from different hierarchies .i.e. HashMap inherited from Map interface, which represents key-value pairs form of data. ArrayList is inherited from List interface, which arranges data in sequential manner. Conversion of HashMap to ArrayList is not straight forward, as there is no direct methods in HashMap to do that. Let’s see how to convert HashMap to ArrayList in java using below demo.
As HashMap contains key-value pairs, there are three ways we can convert given HashMap to ArrayList. We can convert key-value pairs into ArrayList or HashMap keys into ArrayList or HashMap values into ArrayList.
HashMap Key-Value Pairs Into ArrayList
Using entrySet() method of HashMap which returns Set of Entry<K, V> objects, where each Entry object represents one key-value pair. Using this Set create ArrayList.
//Creating a HashMap object HashMap<String, String> demoMap = new HashMap<String, String>(); //Set of entries Set<Entry<String, String>> demoEntrySet = demoMap.entrySet(); //Creating an ArrayList using Set data ArrayList<Entry<String, String>> demoList = new ArrayList<Entry<String,String>>(demoEntrySet);
HashMap Values Into ArrayList
Using values() method of HashMap which returns Collection containing all values of HashMap. Using this Collection create ArrayList
//Creating HashMap object HashMap<String, String> demoMap = new HashMap<String, String>(); //Getting values from HashMap Collection<String> demoValues = demoMap.values(); //Creating an ArrayList of values ArrayList<String> listOfValues = new ArrayList<String>(demoValues);
HashMap Keys Into ArrayList
Using keySet() method of HashMap which returns Set containing all keys of HashMap. Using this Set create ArrayList
//Creating HashMap object HashMap<String, String> demoMap = new HashMap<String, String>(); //Getting Set of keys from HashMap Set<String> demoKey = demoMap.keySet(); //Creating an ArrayList of keys by passing the demoKey ArrayList<String> listOfKeys = new ArrayList<String>(demoKey);
Below code converts the productMap to productListOfEntry, productListKeys, and productListValues.
package test; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Map.Entry; import java.util.Set; /** * Class for converting Map to ArrayList * * Created by MS on 03/25/2016. */ public class HashMapToArrayListDemo { public static void main(String[] args) { //Creating HashMap object HashMap<String, String> productMap = new HashMap<String, String>(); //Adding elements to HashMap productMap.put("SKU123", "Red"); productMap.put("SKU456", "Blue"); productMap.put("SKU678", "Black"); productMap.put("SKU901", "Green"); productMap.put("SKU234", "Yellow"); //Getting entries Set<Entry<String, String>> productEntry = productMap.entrySet(); //Creating an ArrayList Of Entry objects ArrayList<Entry<String, String>> productListOfEntry = new ArrayList<Entry<String,String>>(productEntry); System.out.println("productListOfEntry :"); for (Entry<String, String> entry : productListOfEntry) { System.out.println(entry.getKey()+" : "+entry.getValue()); } System.out.println("####################################################"); //Getting keys Set<String> keyProductSet = productMap.keySet(); //Creating an ArrayList of keys ArrayList<String> productListKeys = new ArrayList<String>(keyProductSet); System.out.println("productListKeys :"); for (String key : productListKeys) { System.out.println(key); } System.out.println("####################################################"); //Getting values Collection<String> productListValues = productMap.values(); //Creating an ArrayList of values ArrayList<String> productListOfValues = new ArrayList<String>(productListValues); System.out.println("productListOfValues :"); for (String value : productListOfValues) { System.out.println(value); } } }
Output:
productListOfEntry : SKU678 : Black SKU456 : Blue SKU901 : Green SKU234 : Yellow SKU123 : Red #################################################### productListKeys : SKU678 SKU456 SKU901 SKU234 SKU123 #################################################### productListOfValues : Black Blue Green Yellow Red