简体   繁体   English

我如何使用Java流来避免此代码发生突变?

[英]How do I use java streams to avoid mutation on this code?

I have a piece of code that is functioning the way I need it to. 我有一段代码可以按照我需要的方式运行。 But I was curious if it can be written better using java streams to avoid mutability. 但是我很好奇它是否可以使用java流更好地编写以避免可变性。

public void method(List<Map<String, String>> list)
{
  List<Map<String, Object>> expectedList = new ArrayList<>();

    for (Map<String, String> entry : list)
    {
        // assume replaceValues(entry) does some logic and returns an updated map
        Map<String, Object> updatedMap = replaceValues(entry);
        expectedList.add(updatedMap);
    }
}

How can I use java streams here? 我如何在这里使用Java流?

list.stream().map(this::replaceValues).collect(Collectors.toList()); 。list.stream()地图(这个:: replaceValues).collect(Collectors.toList());

This is small refactoring, but what do you mean "avoid mutations"? 这是小规模的重构,但是“避免突变”是什么意思?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM