Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • DSA
  • Data Structures
  • Array
  • String
  • Linked List
  • Stack
  • Queue
  • Tree
  • Binary Tree
  • Binary Search Tree
  • Heap
  • Hashing
  • Graph
  • Trie
  • Segment Tree
  • Disjoint Set Union
  • Fenwick Tree
  • AVL Tree
  • Red-Black Tree
  • Advanced Data Structures
Open In App
Next Article:
How to Convert HashMap to ArrayList in Java?
Next article icon

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 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);     } } 

Output
Keys: [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);     } } 

Output
Values: [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);     } } 

Output
Key-Value Pairs: [Geek3=300, Geek2=200, Geek1=100] 

Next Article
How to Convert HashMap to ArrayList in Java?

K

KaashyapMSK
Improve
Article Tags :
  • Java
  • Data Structures
  • DSA
  • Java-Collections
  • Java-ArrayList
  • Java-HashMap
Practice Tags :
  • Data Structures
  • Java
  • Java-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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences