Conversion of Map to List in Java 8

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

 

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
  2. Converting Map values into List of values
  3. Converting Map entries into List of entries

Read Conversion of Map to List in Java until Java 1.7 version

 

1. Convert Map keys to List using keySet() method of Map interface

ConvertMapKeysToListInJava8.java

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

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

public class ConvertMapKeysToListInJava8 {

	// 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");

		// Convert Map keys to List using Java 8 Streams
		List<Integer> rankList = rankCompany.entrySet().stream()
				.map(rank -> rank.getKey()).collect(Collectors.toList());

		System.out.println("Converting Map KEYS into List using Java 8 Streams\n");

		// printing to console using Java 8
		rankList.forEach(System.out::println);

		// 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 using Java 8 Streams

1
2
3
4
5
6
7


Total no. of keys in List is : 7

 

2. Convert Map values to List using values() method of Collection interface

ConvertMapValuesToListInJava8.java

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

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

public class ConvertMapValuesToListInJava8 {

	// 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");

		// Convert Map values to List using Java 8 Streams
		List<String> companyList = rankCompany.entrySet().stream()
				.map(company -> company.getValue()).collect(Collectors.toList());

		System.out.println("Converting Map VALUES into List using Java 8 Streams\n");

		// printing to console using Java 8
		companyList.forEach(System.out::println);

		// 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 using Java 8 Streams

IBM
Microsoft
Accenture
Oracle
Hewlett Packard
SAP
TCS


Total no. of values in List is : 7

 

3. Convert Map entries to List using entrySet() method of Map interface

ConvertMapEntriesToListInJava8.java

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

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javafx.util.Pair;

public class ConvertMapEntriesToListInJava8 {

	// 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");

		// Convert Map entries to List using Java 8 Streams
		List<Pair<Integer, String>> entryList = rankCompany.entrySet()
				.stream().map(e -> new Pair<Integer, String>(e.getKey(), e.getValue()))
				.collect(Collectors.toList());

		System.out.println("Converting Map ENTRIES into List  using Java 8 Streams\n");

		// printing to console using Java 8
		entryList.forEach(System.out::println);

		// 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  using Java 8 Streams

1=IBM
2=Microsoft
3=Accenture
4=Oracle
5=Hewlett Packard
6=SAP
7=TCS


Total no. of entries in List is : 7

 

References:

 

Read Also:

 

Happy Coding !!
Happy Learning !!

Conversion of List to Map in Java
Conversion of Map to List in Java