Java 8 – How to convert HashMap to ArrayList

In this article, we will learn how to convert HashMap entries (key-value pairs) into ArrayList

As Map contains Key-Value pairs, so we can extract

  • keys separately and convert them into ArrayList of Keys
  • values separately and convert them into ArrayList of Values
  • altogether entries and convert them into ArrayList of entries

HashMap to ArrayList conversion :

  1. Convert HashMap keySet and values into ArrayList of Keys and Values respectively using Java 8 Stream
  2. Convert HashMap keySet and values into ArrayList of Keys and Values respectively using inter-conversion constructor
  3. Convert HashMap entrySet to ArrayList of Entries using Java 8 Stream
  4. Convert HashMap entrySet to ArrayList of Entries using inter-conversion constructor

1. Java 8 – HashMap keySet & values to ArrayList of Keys & Values

  • First, we will extract HashMap keys using keySet() method and convert them into ArrayList of Keys using Stream
  • Similarly, we will extract HashMap values using values() method and convert them into ArrayList of values using Stream

ConvertHashMapToArrayListOfKeysAndValuesUsingJava8Stream.java

package net.bench.resources.convert.map.to.list;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class ConvertHashMapToArrayListOfKeysAndValuesUsingJava8Stream {

	public static void main(String[] args) {

		// 1. create HashMap object
		Map<Integer, String> map = new HashMap<>();


		// 1.1 add key-value pairs to HashMap
		map.put(1, "India");
		map.put(2, "Japan");
		map.put(3, "China");
		map.put(4, "Oman");
		map.put(5, "Qatar");


		// 1.2 print to console
		System.out.println("Original HashMap : \n" + map);



		// 2. convert HashMap Keys to ArrayList
		ArrayList<Integer> keyList = map
				.keySet()
				.stream()
				.collect(Collectors.toCollection(ArrayList::new));


		// 2.1 print Map keys
		System.out.println("\n\nArrayList of Map keys : \n" + keyList);


		// 3. convert HashMap Keys to ArrayList
		ArrayList<String> valueList = map
				.values()
				.stream()
				.collect(Collectors.toCollection(ArrayList::new));


		// 3.1 print Map keys
		System.out.println("\n\nArrayList of Map values : \n" + valueList);
	}
}

Output:

Original HashMap : 
{1=India, 2=Japan, 3=China, 4=Oman, 5=Qatar}


ArrayList of Map keys : 
[1, 2, 3, 4, 5]


ArrayList of Map values : 
[India, Japan, China, Oman, Qatar]

2. Before Java 8 – HashMap keySet & values to ArrayList of Keys & Values

  • First, we will extract HashMap keys using keySet() method and convert them into ArrayList of Keys using inter-conversion constructor
  • Similarly, we will extract HashMap values using values() method and convert them into ArrayList of values using inter-conversion constructor

ConvertHashMapToArrayListOfKeysAndValuesUsingConstructor.java

package net.bench.resources.convert.map.to.list;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ConvertHashMapToArrayListOfKeysAndValuesUsingConstructor {

	public static void main(String[] args) {

		// 1. create HashMap object
		Map<Integer, String> map = new HashMap<>();


		// 1.1 add key-value pairs to HashMap
		map.put(1, "India");
		map.put(2, "Japan");
		map.put(3, "China");
		map.put(4, "Oman");
		map.put(5, "Qatar");


		// 1.2 print to console
		System.out.println("Original HashMap : \n" + map);



		// 2. get HashMap keySet
		Set<Integer> keySet = map.keySet();


		// 2.1 convert HashMap keySet to ArrayList using constructor
		List<Integer> keyList = new ArrayList<>(keySet);


		// 2.2 print ArrayList of Map keys
		System.out.println("\n\nArrayList of Map keys : \n" + keyList);



		// 3. get HashMap values
		Collection<String> values = map.values();


		// 3.1 convert HashMap values to ArrayList using constructor
		List<String> valueList = new ArrayList<>(values);


		// 3.2 print ArrayList of Map keys
		System.out.println("\n\nArrayList of Map values : \n" + valueList);
	}
}

Output:

Original HashMap : 
{1=India, 2=Japan, 3=China, 4=Oman, 5=Qatar}


ArrayList of Map keys : 
[1, 2, 3, 4, 5]


ArrayList of Map values : 
[India, Japan, China, Oman, Qatar]

3. Java 8 – HashMap entrySet to ArrayList of Entries

  • We will extract HashMap entries using entrySet() method and convert them into ArrayList of Entries using Stream

ConvertHashMapToArrayListOfEntriesUsingJava8Stream.java

package net.bench.resources.convert.map.to.list;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

public class ConvertHashMapToArrayListOfEntriesUsingJava8Stream {

	public static void main(String[] args) {

		// 1. create HashMap object
		Map<Integer, String> map = new HashMap<>();


		// 1.1 add key-value pairs to HashMap
		map.put(1, "India");
		map.put(2, "Japan");
		map.put(3, "China");
		map.put(4, "Oman");
		map.put(5, "Qatar");


		// 1.2 print to console
		System.out.println("Original HashMap : \n" + map);



		// 2. convert HashMap entries to ArrayList of Entries
		ArrayList<Map.Entry<Integer, String>> entryList = map
				.entrySet()
				.stream()
				.collect(Collectors.toCollection(ArrayList::new));


		// 2.1 print Map keys
		System.out.println("\n\nArrayList of Map entries : \n" + entryList);
	}
}

Output:

Original HashMap : 
{1=India, 2=Japan, 3=China, 4=Oman, 5=Qatar}


ArrayList of Map entries : 
[1=India, 2=Japan, 3=China, 4=Oman, 5=Qatar]

4. Before Java 8 – HashMap entrySet to ArrayList of Entries

  • We will extract HashMap entries using entrySet() method and convert them into ArrayList of Entries using inter-conversion constructor

ConvertHashMapToArrayListOfEntriesUsingConstructor.java

package net.bench.resources.convert.map.to.list;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ConvertHashMapToArrayListOfEntriesUsingConstructor {

	public static void main(String[] args) {

		// 1. create HashMap object
		Map<Integer, String> map = new HashMap<>();


		// 1.1 add key-value pairs to HashMap
		map.put(1, "India");
		map.put(2, "Japan");
		map.put(3, "China");
		map.put(4, "Oman");
		map.put(5, "Qatar");


		// 1.2 print to console
		System.out.println("Original HashMap : \n" + map);



		// 2. get HashMap entrySet
		Set<Map.Entry<Integer, String>> entries = map.entrySet();


		// 2.1 convert HashMap keySet to ArrayList using constructor
		List<Map.Entry<Integer, String>> entryList = new ArrayList<>(entries);


		// 2.2 print ArrayList of Map keys
		System.out.println("\n\nArrayList of Map entries : \n" + entryList);
	}
}

Output:

Original HashMap : 
{1=India, 2=Japan, 3=China, 4=Oman, 5=Qatar}


ArrayList of Map entries : 
[1=India, 2=Japan, 3=China, 4=Oman, 5=Qatar]

Read Also:

References:

Happy Coding !!
Happy Learning !!

Java 8 – Iterating HashMap in 8 ways