[英]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". 我有一个Map“M”和一个列表“L”,现在我想使用“L”中可用的那些键列表从该映射“M”获取值。 I want to use java 8 Stream concept can anyone help on this.
我想使用java 8 Stream概念可以有人帮忙。
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));}); 。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
每个元素map
到Map
的相应值并收集到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
). 您可能需要考虑消除
null
值(由Map
不存在的键产生)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.