How to Convert HashMap to ArrayList in Java? Last Updated : 12 Feb, 2025 Comments Improve Suggest changes Like Article Like Report 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 ArrayList1. Converting HashMap keys to an ArrayListIf 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 ArrayListIf 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 ArrayListIf 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] Comment More infoAdvertise with us Next Article How to Convert HashMap to ArrayList in Java? K KaashyapMSK Follow Improve Article Tags : Java Data Structures DSA Java-Collections Java-ArrayList Java-HashMap +2 More Practice Tags : Data StructuresJavaJava-Collections 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 HashMap forEach(BiConsumer) method in Java with Examples The forEach(BiConsumer) method of HashMap class perform the BiConsumer operation on each entry of hashmap until all entries have been processed or the action throws an exception. The BiConsumer operation is a function operation of the key-value pair of hashtable performed in the order of iteration. 3 min read Convert List to Set in Java Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element. Few important features of the Java Set interface are as follows: The set interface is an unordered collection of objec 5 min read HashMap and TreeMap in Java HashMap and TreeMap are part of collection framework. HashMapjava.util.HashMap class is a Hashing based implementation. In HashMap, we have a key and a value pair<Key, Value>. HashMap<K, V> hmap = new HashMap<K, V>(); Let us consider below example where we have to count occurrences 5 min read Methods to Create Preallocated HashMap in Java 19 Java 19 has introduced some new methods to create preallocated HashMaps which can enhance the performance of your application. In this article, we will explore the concept of preallocated HashMaps and how to create them using the new methods in Java 19. HashMap is a widely used data structure in Jav 3 min read Conversion of Java Map to List In Java, a Map is a collection that maps keys to values and a list is an ordered collection of objects and the List can contain duplicate values. There are some scenarios where we need to convert a Map into a List. In this article, we will explore how to convert the keys, values, or entries from a M 3 min read Like