簡體   English   中英

如何使用Java流從HashMaps的ArrayList中獲取字符串

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

我有一個數據結構-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"));
}

我們如何使用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