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,
- Converting Map keys into List of keys using keySet(); of Map
- Converting Map values into List of values using values(); of Collection
- 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:
- Java – Conversion of List to Map
- Java – Conversion of Map to List
- Java – Conversion of Arrays to List
- Java – Conversion of List to Arrays
- Java 8 – Conversion of List to Map
- Java 8 – Conversion of Map to List
- Java 8 – Conversion of Arrays to List
- Java 8 – Conversion of List to Arrays
- Java 8 – How to convert HashMap to ArrayList ?
- Java 8 – Conversion of Arrays to Stream
- Java 8 – Conversion of Stream to Arrays
- Java 8 – Convert List to Stream
- Java 8 – Convert Stream to List
- Java 8 – Convert Stream to ArrayList
- Java 8 – Convert Stream to LinkedList
- Java 8 – Convert Stream to HashSet
- Java 8 – Convert Stream to LinkedHashSet
- Java 8 – Convert Stream to TreeSet
- Java 8 – Convert Stream to HashMap
- Java 8 – Convert Stream to LinkedHashMap
- Java 8 – Convert Stream to TreeMap
- Java 8 – Convert Stream to ConcurrentHashMap
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
- https://docs.oracle.com/javase/tutorial/collections/implementations/list.html
- https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/map.html
- https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html
Happy Coding !!
Happy Learning !!