Pulling a List of Values from a Map given a List of Keys on Java 8

jiveturkey :

I feel embarrassed that I am stuck on this but I am trying to pull the List of Strings (List<String>) from the Map<MyEnum, String> given then List of enum keys List<MyEnum>. The List<MyEnum> may or may not contain entries.

Edit:

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

for (MyEnum field : fields) {
    String value = null;
    if ((value = map.get(field)) != null) {
       toReturn.add(value);
    }
}
return toReturn;

But I am looking for a Java 8 way to do this. Such as...

map.stream().map(e->?????)
Eugene :
fields.stream()
      .map(map::get)
      .filter(Objects::nonNull)
      .collect(Collectors.toList())

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=475962&siteId=1