HasMap list evaluation using java 8

 


   We will evaluate the    HashMap with the list as a value.  In this

  code, I  just take simple lists for example.  Code  is simple 



import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.stream.Collectors;


public class EvaluteHashMap {


public static void  mapValue() {



Map<Integer, List> id1 = new HashMap<Integer,List>();


List<String> list1 = new ArrayList<String>();


list1.add("r1");

list1.add("r4");


List<String> list2 = new ArrayList<String>();

list2.add("r2");

list2.add("r5");


List<String> list3 = new ArrayList<String>();

list3.add("r3");

list3.add("r6");


id1.put(1,list1);

id1.put(2,list2);

id1.put(3,list3);

id1.put(10,list2);

id1.put(15,list3);

/* List<List> list = 

id1.entrySet() .stream() .filter(e -> e.getKey() == 1 )

  .map(Map.Entry::getValue) .collect(Collectors.toList());

*/



List<List> list=id1.keySet().stream()

        .filter(x -> x == 1)

        .map(x -> id1.get(x))

        .collect(Collectors.toList());

System.out.println("Value of list X=1::" +list);

}

public static void main(String args[]) {

 

 

    mapValue();

  }

}



Program output 


Value of list X=1::[[r1, r4]]

HasMap list evaluation using java 8  HasMap  list evaluation using java 8 Reviewed by Mukesh Jha on 6:37 AM Rating: 5

No comments:

Add your comment

All Right Reserved To Mukesh Jha.. Theme images by Jason Morrow. Powered by Blogger.