我必須遍歷一組鍵/值對并修改每個鍵(不是值)以刪除前置字符串。我試圖在一個聲明中做到這一點,但需要一點幫助。我確定我需要.map()做手術(shù),但我無法進行。在嘗試任何轉(zhuǎn)換之前,這是我的代碼:Map<String, String> properties = configs.stream()
.flatMap(config -> config.getProperties().entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (left, right) -> right));所以我天真的解決方案是包裝Map.Entry::getKey在一個String函數(shù)中,但我收到一個編譯錯誤,說需要一個函數(shù)式接口。有什么我可以在這里使用的開箱即用的東西,還是我真的需要實現(xiàn)我自己的接口來擺脫字符串?
1 回答
一只名叫tom的貓
TA貢獻1906條經(jīng)驗 獲得超3個贊
代替
Collectors.toMap(
Map.Entry::getKey,
...
您可以使用
Collectors.toMap(
e -> StringUtils.removeStart(e.getKey(), "prefix to remove"),
...
或者
Collectors.toMap(
e -> e.getKey().substring("prefix to remove".length()),
...
如果您沒有 Apache Commons 依賴項
添加回答
舉報
0/150
提交
取消
