Java 8: EntrySet ストリームの値を変更する 質問する

Java 8: EntrySet ストリームの値を変更する 質問する

次のような設定になっています:

Map<Instant, String> items;
...
String renderTags(String text) {
    // Renders markup tags in a string to human readable form
}
...
<?> getItems() {
    // Here is where I need help
}

問題は、itemsマップの値である文字列がタグでマークアップされていることです。getItems()メソッドを使用して解析された文字列を使用して、すべての項目を返したいのですrenderTags(String)。次のようになります。

// Doesn't work
items.entrySet().stream().map(e -> e.setValue(renderTags(e.getValue())));

これを行う最も効果的な方法は何ですか?

ベストアンサー1

結果が必要な場合は、Map次の手順に従ってください。

Map<Instant, String> getItems() {
    return items.entrySet()
            .stream()
            .collect(Collectors.toMap(
                    Map.Entry::getKey,
                    e -> renderTags(e.getValue())));
}

おすすめ記事