Pra*_*dra 2 java hashmap java-stream
我有一个数据结构 - 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"));
}
Run Code Online (Sandbox Code Playgroud)
我们如何使用Java Streams实现相同的目标?
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())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
75 次 |
| 最近记录: |