How many ways can convert HashMap to List in Java and using Java8
Hi,
Today we will talk about how we can convert Hashmap into ArrayList.
1. we have converted keys of HashMap into List.
2. we have converted all values of Map into List.
3. we have converted all entries(key & values) of HashMap into List.
In below i have explained program stepwise:-
package com.neesri.sorting;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
public class ConvertMapToList {
// we can convert Hashmap into Arraylist through three ways
public static void main(String[] args) {
HashMap<String, Integer> hashmapObj = new HashMap<>();
hashmapObj.put("Zimbabwe", 600);
hashmapObj.put("India", 700);
hashmapObj.put("SriLanka", 400);
hashmapObj.put("Nepal", 800);
// 1-----convert Hashmap Key into List
// using keyset method retun set view
Set<String> setViewOfMapKeys = hashmapObj.keySet();
// convert set into list
List<String> listObj = new ArrayList<>(setViewOfMapKeys);
System.out.println("Hashmap Keys into list-------------------------------->");
for (String printList : listObj) {
System.out.println(printList);
}
// ---------------------using java8 stream -----------------------------
System.out.println("Hashmap Keys into list using Java8 Stream ------------->");
List<String> listJava8_forKey = hashmapObj.keySet().stream().collect(Collectors.toList());
listJava8_forKey.forEach(System.out::println);
// 2. Convert Hashmap Values into List
Collection<Integer> values = hashmapObj.values();
List<Integer> list = new ArrayList<>(values);
System.out.println("Hashmap values into List-------------------------------->");
for (Integer hashmapValuesIntoList : list) {
System.out.println(hashmapValuesIntoList);
}
// ---------------------using java8 stream -----------------------------
System.out.println("Hashmap values into List using Java8 Stream---------------->");
List<Integer> listjava8_forValues = hashmapObj.values().stream().collect(Collectors.toList());
listjava8_forValues.forEach(System.out::println);
// 3. Convert hashmap all entries into List
Set<Entry<String, Integer>> entriesObjSet = hashmapObj.entrySet();
List<Entry<String, Integer>> listEntries = new ArrayList<>(entriesObjSet);
// System.out.println("Hashmap all entries into List======>");
/*
* for(Object obj:listEntries) {
*
* System.out.println(obj); }
*/
// OR
/*
* for(Entry<String, Integer> obj:listEntries) {
*
* System.out.println(obj); }
*/
// OR
System.out.println("Printing thorough iterator======================>");
Iterator<Entry<String, Integer>> iterator = listEntries.iterator();
while (iterator.hasNext()) {
Entry<String, Integer> entry = iterator.next();
System.out.println(entry);
}
//
System.out.println("Hashmap ALL entries thorugh JAVA 8 Stream===============>");
List<Entry<String, Integer>> listjava8_allentries = hashmapObj.entrySet().stream().collect(Collectors.toList());
listjava8_allentries.forEach(System.out::println);
}
}
output → Hashmap Keys into list — — — — — — — — — — — — — — — →
SriLanka
Zimbabwe
Nepal
India
Hashmap Keys into list using Java8 Stream — — — — — — ->
SriLanka
Zimbabwe
Nepal
India
Hashmap values into List — — — — — — — — — — — — — — — →
400
600
800
700
Hashmap values into List using Java8 Stream — — — — — — — →
400
600
800
700
Printing thorough iterator======================>
SriLanka=400
Zimbabwe=600
Nepal=800
India=700
Hashmap ALL entries thorugh JAVA 8 Stream===============>
SriLanka=400
Zimbabwe=600
Nepal=800
India=700