Can we convert ArrayList to map 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 HashMaps Node objects in ArrayList.

Table of Contents

  • Using Java 8s Stream API
    • Java 8: Convert HashMaps keys to ArrayList
    • Convert HashMaps values to ArrayList
    • Convert HashMaps Entry objects to ArrayList
  • Using ArrayLists constructor
    • Convert HashMaps keys to ArrayList
    • Convert HashMaps values to ArrayList
    • Convert HashMaps Entry objects to ArrayList
  • Java HashMap tutorial
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 8s Stream API

Java 8: Convert HashMaps keys to ArrayList

We can use HashMaps keyset[] method to get a set of keys and use Java 8s Stream API to convert it to ArrayList.

Java
1
2
3
4
5
List customerIdList = customerIdNameMap.keySet[]
.stream[]
.collect[Collectors.toList[]];

Convert HashMaps values to ArrayList

We can use HashMaps values[] method to get a collection of values and use Java 8s Stream API to convert it to ArrayList.

1
2
3
4
5
List customerNames = customerIdNameMap.values[]
.stream[]
.collect[Collectors.toList[]];

Convert HashMaps Entry objects to ArrayList

We can use HashMaps entrySet[] method to get set of Entry object and use Java 8s Stream API to convert it to ArrayList.

1
2
3
4
5
List entryCustomerList = customerIdNameMap.entrySet[]
.stream[]
.collect[Collectors.toList[]];

Lets see the complete example.

HashMapToArrayListMainJava8.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package org.arpit.java2blog;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
public class HashMapToArrayListMainJava8 {
public static void main[String[] args] {
HashMap customerIdNameMap = new HashMap[];
// Putting key-values pairs in HashMap
customerIdNameMap.put[1001, "Arman"];
customerIdNameMap.put[1002, "Javin"];
customerIdNameMap.put[1003, "Mat"];
customerIdNameMap.put[1004, "Joe"];
// Java 8
// Convert keys to ArrayList
List customerIdList = customerIdNameMap.keySet[]
.stream[]
.collect[Collectors.toList[]];
System.out.println["customerIds: "+customerIdList];
// Convert values to ArrayList
List customerNames = customerIdNameMap.values[]
.stream[]
.collect[Collectors.toList[]];
System.out.println["Customer Names: "+ customerNames];
// Convert entry objects to ArrayList
List entryCustomerList = customerIdNameMap.entrySet[]
.stream[]
.collect[Collectors.toList[]];
System.out.println["Customer ID and Names: "+entryCustomerList];
}
}

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 ArrayLists constructor

Convert HashMaps keys to ArrayList

Get keySet[] from HashMap and then convert it to ArrayList using its constructor.

1
2
3
4
5
// Convert keys to ArrayList
Set keySet = customerIdNameMap.keySet[];
List customerIdList = new ArrayList[keySet];

Convert HashMaps values to ArrayList

Get values[]from HashMap and then convert it to ArrayList using its constructor.

1
2
3
4
5
// Convert values to ArrayList
Collection values = customerIdNameMap.values[];
List customerNames = new ArrayList[values];

Convert HashMaps Entry objects to ArrayList

Get entrySet[] from HashMap and then convert it to ArrayList using its constructor.

1
2
3
4
5
// Convert entry objects to ArrayList
Set entrySet = customerIdNameMap.entrySet[];
List entryCustomerList = new ArrayList[entrySet];
Here is the complete program to convert HashMap to ArrayList.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package org.arpit.java2blog;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class HashMapToArrayListMain {
public static void main[String[] args] {
HashMap customerIdNameMap = new HashMap[];
// Putting key-values pairs in HashMap
customerIdNameMap.put[1001, "Arman"];
customerIdNameMap.put[1002, "Javin"];
customerIdNameMap.put[1003, "Mat"];
customerIdNameMap.put[1004, "Joe"];
// Before Java 8
// Convert keys to ArrayList
Set keySet = customerIdNameMap.keySet[];
List customerIdList = new ArrayList[keySet];
System.out.println["customerIds: "+customerIdList];
// Convert values to ArrayList
Collection values = customerIdNameMap.values[];
List customerNames = new ArrayList[values];
System.out.println["Customer Names: "+ customerNames];
// Convert entry objects to ArrayList
Set entrySet = customerIdNameMap.entrySet[];
List entryCustomerList = new ArrayList[entrySet];
System.out.println["Customer ID and Names: "+entryCustomerList];
}
}

Java HashMap tutorial

  • HashMap in java
  • HashMap internal working
  • hash and indexfor method in HashMap
  • hashcode and equals in javasort
  • HashMap by keys and values
  • Difference between HashMap and HashSet
  • Difference between HashMap and Hashtable
  • How to iterate over HashMap
Thats all about How to convert HashMap to ArrayList.

import_contacts

You may also like:

How to remove element from Arraylist in java while iterating

Print HashMap in Java

Print LinkedList in java

Java Set to Array

Print ArrayList in Java

How to Deep Copy Arraylist in Java

Initialize ArrayList with values in Java

PriorityQueue in Java 8

2d Arraylist java example

How HashMap works in java

  • 1
  • 2
  • 3
  • 5

Video liên quan

Chủ Đề