简体   繁体   中英

How to get values from a Map with a list of keys using Java8 streams

I have a Map "M" and a list "L", now i want get the values from that map "M" using those list of keys available in "L". I want to use java 8 Stream concept can anyone help on this.

I coded to print those values but what i need is to get values into a list

list.stream().forEach(s->{System.out.println(map.get(s));});

map each element of the List to the corresponding value in the Map and collect to a List :

List<String> values =
    list.stream()
        .map(map::get)
        .collect(Collectors.toList());

You might want to consider eliminating null values (which result from keys not present in the Map ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM