简体   繁体   English

如何使用Java流从HashMaps的ArrayList中获取字符串

[英]How to use Java Streams to fetch Strings from an ArrayList of HashMaps

I have a Data structure - ArrayList> and this is what I need to do - 我有一个数据结构-ArrayList>,这是我需要做的-

ArrayList<HashMap<String, String>> optMapList;
//populated optMapList with some code. Not to worry abt this

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

for(HashMap<String,String> entry: optMapList){
    values.add(entry.get("optValue"));
}

How do we use Java Streams to achieve the same objective? 我们如何使用Java流实现相同的目标?

 optMapList.stream()
           .filter(Objects:nonNull) // potentially filter null maps
           .map(m -> m.get("optValue"))
           .filter(Objects::nonNull) // potentially filter null values form the map 
           // .collect(Collectors.toCollection(ArrayList::new)) 
           .collect(Collectors.toList())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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