How to Convert HashMap to ArrayList in Java?
Last Updated : 12 Feb, 2025
In Java a HashMap is a collection that stores key-value pairs on the other hand, an ArrayList is a collection that stores dynamic arrays. There are some scenarios where we need to convert a HashMap into an ArrayList such as:
- Extracting only the keys or values in the list form.
- Converting key-value pairs into a list of objects for further processing.
Methods to Convert HashMap to an ArrayList
1. Converting HashMap keys to an ArrayList
If we need an ArrayList containing only the keys from the HashMap, we can use the keySet() method in the HashMap and convert it to an ArrayList.
Example: This example demonstrates how to extract the keys of a HashMap and store them in an ArrayList.
Java // Java Program to demonstrates // the working of keySet() import java.util.*; public class Geeks { public static void main(String[] args) { HashMap<String, Integer> m = new HashMap<>(); m.put("Geek1", 100); m.put("Geek2", 200); m.put("Geek3", 300); ArrayList<String> al = new ArrayList<>(m.keySet()); System.out.println("Keys: " + al); } }
OutputKeys: [Geek3, Geek2, Geek1]
2. Converting HashMap values to an ArrayList
If we need an ArrayList containing only the values from the HashMap, we can use the values() method in the HashMap and convert it to an ArrayList.
Example: This example demonstrates how to extract the values of a HashMap and store them in an ArrayList.
Java // Java Prpgram to demonstrates the working of values() import java.util.*; public class Geeks { public static void main(String[] args) { HashMap<String, Integer> m = new HashMap<>(); m.put("Geek1", 100); m.put("Geek2", 200); m.put("Geek3", 300); ArrayList<Integer> al = new ArrayList<>(m.values()); System.out.println("Values: " + al); } }
OutputValues: [300, 200, 100]
3. Converting HashMap key-value pairs to an ArrayList
If we want to keep a relationship between each key paired with its value, we can use the entrySet() method, this method returns a set of key value pairs and we can convert it into an ArrayList to work with it like a list.
Example: This example demonstrates how to convert the key-value pairs of a HashMap into an ArrayList of Map.Entry Objects.
Java // Java Program to demonstrates // the working of entrySet() import java.util.*; public class Geeks { public static void main(String[] args) { HashMap<String, Integer> m = new HashMap<>(); m.put("Geek1", 100); m.put("Geek2", 200); m.put("Geek3", 300); // Convert HashMap key-value pairs into an ArrayList ArrayList<Map.Entry<String, Integer>> al = new ArrayList<>(m.entrySet()); // Print the ArrayList containing key-value pairs System.out.println("Key-Value Pairs: " + al); } }
OutputKey-Value Pairs: [Geek3=300, Geek2=200, Geek1=100]
Similar Reads
Array to ArrayList Conversion in Java In Java, arrays are fixed-sized, whereas ArrayLists are part of the Java collection Framework and are dynamic in nature. Converting an array to an ArrayList is a very common task and there are several ways to achieve it.Methods to Convert Array to an ArrayList1. Using add() Method to Manually add th
3 min read
Java Program to Convert ArrayList to LinkedList Given an array list, your task is to write a program to convert the given array list to Linked List in Java. Examples: Input: ArrayList: [Geeks, forGeeks, A computer Portal] Output: LinkedList: [Geeks, forGeeks, A computer Portal] Input: ArrayList: [1, 2, 3, 4, 5] Output: LinkedList: [1, 2, 3, 4, 5]
6 min read
HashMap entrySet() Method in Java The entrySet() method of the HashMap class in Java is used to create a set view of the mappings contained in the HashMap. This method allows us to iterate over the key-value pairs in the map or convert them into a set.Example 1: Here, we will use the entrySet() method to view the mappings in a HashM
2 min read
HashMap clear() Method in Java The clear() method of the HashMap class in Java is used to remove all of the elements or mappings (key-value pairs) from a specified HashMap.Example 2: Here, we will use the clear() method to clear a HashMap of Integer keys and String values. Java// Clearing HashMap of Integer keys // and String val
2 min read
CopyOnWriteArrayList hashCode() method in Java The hashCode() method of CopyOnWriteArrayList returns the hashCode value of the list. Syntax: public int hashCode() Parameters: The function does not accepts any parameter. Return Value: The function returns the hashCode value of the list. Below programs illustrate the above function: Program 1: Jav
1 min read