How to convert Map in to List .
This program explain how you can get a HashMap in to list of elements using
Java 8. and also explain frequency of the element in to the Collections.
Java 8. and also explain frequency of the element in to the Collections.
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.ArrayList;
public class ConvertMapToList {
public static void main(String[] args) {
Map map = new HashMap<>();
map.put(10, "apple");
map.put(20, "orange");
map.put(30, "banana");
map.put(40, "Mango");
map.put(50, "Papaya");
List arrlist = new ArrayList();
arrlist.add("A");
arrlist.add("B");
arrlist.add("C");
arrlist.add("C");
arrlist.add("C");
System.out.println("Getting repetation of object");
System.out.println("List"+arrlist);
int i = Collections.frequency(arrlist, "C");
System.out.println("Number of repetation of C::"+i);
System.out.println("Getting sorted Map Key and Value list");
List result= map.entrySet().stream().map(x->x.getKey()).collect(Collectors.toList());
List mapValues=map.entrySet().stream().map(x->x.getValue()).collect(Collectors.toList());
List ar=new ArrayList(mapValues);
Collections.sort(ar);
System.out.println("\n1. Export Map Value to List...");
ar.forEach(System.out::println);
System.out.println("\n2. Export Map Key to List...");
result.forEach(System.out::println);
}
}
Program Output :
Getting repetation of object
List[A, B, C, C, C]
Number of repetation of C::3
Getting sorted Map Key and Value list
1. Export Map Value to List...
Mango
Papaya
apple
banana
orange
2. Export Map Key to List...
50
20
40
10
30
How to convert Map in to List .
Reviewed by Mukesh Jha
on
12:28 AM
Rating:
No comments:
Add your comment