Convert HashMap to ArrayList in java

In this post, we will see how to convert HashMap to ArrayList in java. Many times, you need to store keys or values into ArrayList or Store HashMap’s Node objects in ArrayList.

HashMap and ArrayList both are the most used data structure in java and both have different Hierarchy.

Here are the ways to convert HashMap to ArrayList in java.

Using Java 8’s Stream API

Java 8: Convert HashMap’s keys to ArrayList

We can use HashMap’s keyset()  method to get a set of keys and use Java 8‘s Stream API to convert it to ArrayList.

Convert HashMap’s values to ArrayList

We can use HashMap’s values() method to get a collection of values and use Java 8’s Stream API to convert it to ArrayList.

Convert HashMap’s Entry objects to ArrayList

We can use HashMap’s entrySet() method to get set of Entry object and use Java 8’s Stream API to convert it to ArrayList.

Let’s see the complete example.

Output:

customerIds: [1001, 1002, 1003, 1004] Customer Names: [Arman, Javin, Mat, Joe] Customer ID and Names: [1001=Arman, 1002=Javin, 1003=Mat, 1004=Joe]

Using ArrayList’s constructor

Convert HashMap’s keys to ArrayList

Get keySet()  from HashMap and then convert it to ArrayList using its constructor.

Convert HashMap’s values to ArrayList

Get values() from HashMap and then convert it to ArrayList using its constructor.

Convert HashMap’s Entry objects to ArrayList

Get entrySet()  from HashMap and then convert it to ArrayList using its constructor.

Here is the complete program to convert HashMap to ArrayList.

import_contacts

You may also like:

Related Posts

  • 10 September

    How to remove element from Arraylist in java while iterating

    Table of ContentsIntroductionUsing Collection’s removeIf() methodUsing ListIterator classUsing removeAll() methodUsing Java 8 Stream to filter List itemsConclusion Introduction In this tutorial, you will learn how to remove element from Arraylist in java while iterating using different implementations provided by Java. It is necessary to understand the right way to remove items from a List because […]

  • Print HashMap in java
    30 June

    Print HashMap in Java

    Table of ContentsPrint from the HashMap ReferencePrint HashMap using foreach method with keyset()Print HashMap using Consumer with entrySet()Print HashMap using Arrays’s asList() methodPrint HashMap using Collections’s singletonList()Print HashMap using getkey() and getValue with entrySet()Print HashMap using BiConsumerPrint HashMap using IteratorPrint HashMap using custom ObjectsConclusion In this article, we will see how to print HashMap in […]

  • Print LinkedList in java
    26 June

    Print LinkedList in java

    Table of ContentsJava inbuilt LinkedListPrint LinkedList using a for loopPrint LinkedList using Enhanced for loopPrint LinkedList using streamsPrint LinkedList using toString() methodPrint LinkedList using IteratorPrint LinkedList using custom objectsCustom singly LinkedListHow to print a custom singly LinkedListConclusion Java inbuilt LinkedList Java inbuilt LinkedList class uses doubly linked list as internal data structure to store its […]

  • Java Set to Array
    12 May

    Java Set to Array

    Table of Contents1. Using Java 8’s Stream2. Using toArray()3. Using toArray(IntFunction) [Java 11]4. Using System.arraycopy()5. Using Arrays.copyOf6. Using simple iteration7. Using Guava library7.1 Using FluentIterable7.2 Using Iterables In this post, we will learn java set to array conversion. There are many ways to convert set to an array. 1. Using Java 8’s Stream If you […]

  • Print ArrayList in java
    11 May

    Print ArrayList in Java

    Learn about how to print ArrayList in java in different ways.

  • 15 April

    How to Deep Copy Arraylist in Java

    Learn about how to create deep copy of ArrayList in different ways.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.