[英]Create Hashmap using string array as value and Enum as Key in Java 8 Lambda-Stream
[英]Filtering on Java Stream based on Index or last array value modification using lambda
我有下面的代碼中提到的String數組。 我消除了每個元素中存在的雙引號,但是我必須對last Index執行附加操作,因為我知道還有附加的雙引號。
String array[] = new String[]{"\"Awesome Systems, Inc. - 357", "\"Awesome Systems, Inc - 357", "\"wpe_stage\""};
Arrays.stream(array).map((str) -> (str = str.trim())
.substring(1, str.length()))
.forEach(System.out::println);
我得到的是類似的輸出-
Awesome Systems, Inc. - 357
Awesome Systems, Inc - 357
wpe_stage"
所需的輸出類似於-
Awesome Systems, Inc. - 357
Awesome Systems, Inc - 357
wpe_stage
如何使用流功能或lambda做到這一點? 有什么方法可以選擇流中的最后一個索引,也可以基於索引進行任何過濾?
您可以使用replaceAll
並一次性替換雙引號的所有實例,前提是它們位於元素的開頭或結尾:
Arrays
.stream(array)
.map((str) -> (str = str.trim()).replaceAll("^\"|\"$", ""))
.forEach(System.out::println);
輸出量
Awesome Systems, Inc. - 357
Awesome Systems, Inc - 357
wpe_stage
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.