Java – Conversion of Map to List

In this article, we will discuss how to convert Map into List in Java

As Map contains key-value pairs which is called as Map entries, we will convert all possible thing into List like,

  1. Converting Map keys into List of keys using keySet(); of Map
  2. Converting Map values into List of values using values(); of Collection
  3. Converting Map entries into List of entries using entrySet(); of Map

1. Convert Map keys to List using keySet() :

ConvertMapKeysToList.java

package in.bench.resources.java.collection.map;

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

public class ConvertMapKeysToList {

	// main() method - entry point for JVM
	public static void main(String[] args) {

		// creating HashMap object of type <Integer, String>
		Map<Integer, String> rankCompany =
				new HashMap<Integer, String>();

		// adding key-value pairs to HashMap object
		rankCompany.put(1, "IBM");
		rankCompany.put(2, "Microsoft");
		rankCompany.put(3, "Accenture");
		rankCompany.put(4, "Oracle");
		rankCompany.put(5, "Hewlett Packard");
		rankCompany.put(6, "SAP");
		rankCompany.put(7, "TCS");

		// get keys using keySet() method of Map interface
		Set<Integer> setOfKeys = rankCompany.keySet();

		// Convert Map keys to List
		// using inter-conversion collection constructor
		List<Integer> rankList = new ArrayList<Integer>(setOfKeys);

		System.out.println("Converting Map KEYS into List\n");

		// iterating using enhanced for-loop
		for (Integer rank : rankList) {
			System.out.println(rank);
		}

		// print no. of keys present inside List
		System.out.println("\n\nTotal no. of keys in List is : "
				+ rankList.size());
	}
}

Output:

Converting Map KEYS into List

1
2
3
4
5
6
7

Total no. of keys in List is : 7

2. Convert Map values to List using values() :

ConvertMapValuesToList.java

package in.bench.resources.java.collection.map;

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

public class ConvertMapValuesToList {

	// main() method - entry point for JVM
	public static void main(String[] args) {

		// creating HashMap object of type <Integer, String>
		Map<Integer, String> rankCompany =
				new HashMap<Integer, String>(); 

		// adding key-value pairs to HashMap object
		rankCompany.put(1, "IBM");
		rankCompany.put(2, "Microsoft");
		rankCompany.put(3, "Accenture");
		rankCompany.put(4, "Oracle");
		rankCompany.put(5, "Hewlett Packard");
		rankCompany.put(6, "SAP");
		rankCompany.put(7, "TCS");

		// get keys using keySet() method of Map interface
		Collection<String> collectionValues = rankCompany.values();

		// Convert Map values to List
		// using inter-conversion collection constructor
		List<String> companyList = new ArrayList<String>(
				collectionValues);

		System.out.println("Converting Map VALUES into List\n");

		// iterating using enhanced for-loop
		for (String comp : companyList) {
			System.out.println(comp);
		}

		// print no. of values present inside List
		System.out.println("\n\nTotal no. of values in List is : "
				+ companyList.size());
	}
}

Output:

Converting Map VALUES into List

IBM
Microsoft
Accenture
Oracle
Hewlett Packard
SAP
TCS

Total no. of values in List is : 7

3. Convert Map entries to List using entrySet() :

ConvertMapEntriesToList.java

package in.bench.resources.java.collection.map;

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

public class ConvertMapEntriesToList {

	// main() method - entry point for JVM
	public static void main(String[] args) {

		// creating HashMap object of type <Integer, String>
		Map<Integer, String> rankCompany =
				new HashMap<Integer, String>(); 

		// adding key-value pairs to HashMap object
		rankCompany.put(1, "IBM");
		rankCompany.put(2, "Microsoft");
		rankCompany.put(3, "Accenture");
		rankCompany.put(4, "Oracle");
		rankCompany.put(5, "Hewlett Packard");
		rankCompany.put(6, "SAP");
		rankCompany.put(7, "TCS");

		// get keys using keySet() method of Map interface
		Set<Map.Entry>Integer, String<> setOfEntries =
				rankCompany.entrySet();

		// Convert Map entries to List
		// using inter-conversion collection constructor
		List<Map.Entry>Integer, String<> entryList =
				new ArrayList<Map.Entry>Integer, String<>(setOfEntries);

		System.out.println("Converting Map ENTRIES into List\n");

		// iterating using enhanced for-loop
		for (Map.Entry<Integer, String> entry : entryList) {
			System.out.println("Key : " + entry.getKey()
					+ "\t\t Value : " + entry.getValue());
		}

		// print no. of entries present inside List
		System.out.println("\n\nTotal no. of entries in List is : "
				+ entryList.size());
	}
}

Output:

Converting Map ENTRIES into List

Key : 1		 Value : IBM
Key : 2		 Value : Microsoft
Key : 3		 Value : Accenture
Key : 4		 Value : Oracle
Key : 5		 Value : Hewlett Packard
Key : 6		 Value : SAP
Key : 7		 Value : TCS

Total no. of entries in List is : 7

In the following article, we will see how could we achieve the same result using Java 8 version

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 - Conversion of Map to List
Java 8 - Conversion of Arrays to List