简体   繁体   English

如何构建一个新列表,其中包含现有列表中的所有条目以及修改了一个字段的每个条目的副本?

[英]How to build a new list containing all entries from existing list AND a copy of each entry with one field modified?

I have a list of some Object called list.我有一个名为列表的 Object 列表。 Using list.stream(), I need to create a new list of the same Object where the new list contains all of the original entries AND the new list contains a copy of each entry with one field modified.使用 list.stream(),我需要创建一个相同 Object 的新列表,其中新列表包含所有原始条目,并且新列表包含每个条目的副本,其中一个字段已修改。 I know how to do this using list.stream() twice but I'd like to do it under 1 stream.我知道如何使用 list.stream() 两次,但我想在 1 stream 下进行。

This is how I've accomplished the task using list.stream() twice这就是我使用 list.stream() 两次完成任务的方式

         newList = list.stream().collect(Collectors.toList());
         newList.addAll(list.stream().map(l -> {SomeObject a = new SomeObject(l);
                              a.setField1("New Value");
                              return a;
                              }).collect(Collectors.toList())
                        );

Use flatMap (assuming you don't mind the original and derived values being interleaved):使用flatMap (假设您不介意交错的原始值和派生值):

newList = list.stream()
    .flatMap(l -> {
      SomeObject a = new SomeObject(l);
      a.setField1("New Value");
      return Stream.of(l, a);
    })
    .collect(toList());

If it's just that you don't want to use stream() twice, you could avoid the first one using addAll ;如果只是不想使用stream()两次,则可以避免使用第一个addAll and the unnecessary collect for the second:以及第二个不必要的collect

newList = new ArrayList<>(list.size() * 2);
newList.addAll(list);
list.stream()
    .map(l -> {
        SomeObject a = new SomeObject(l);
        a.setField1("New Value");
        return a;
    })
    .forEach(newList::add);

If you want objct then processed object and so on, in your new list then, you can try something like this:如果你想要 objct 然后处理 object 等等,那么在你的新列表中,你可以尝试这样的事情:

List<String> list = List.of("a", "b", "c" );
        
List<String> answer = list.stream()
             .map(s -> List.of(s,s+"1"))
             .flatMap(List::stream).collect(Collectors.toList());
        
System.out.println(answer);

Output: Output:

[a, a1, b, b1, c, c1]

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

相关问题 条目列表,如何添加新条目? - List of entries, how to add a new Entry? 复制包含列表中单词之一的文件 - Copy files containing one of the word from list 如何获取所有列表的列表,这些列表恰好包含列表列表中每个列表的一个元素 - How to get a list of all lists containing exactly one element of each list of a list of lists 创建条目列表并使每个条目可单击 - Create a list of entries and make each entry clickable 通过与一个条目进行比较来查找列表中最接近的条目 - Find closest entries in a list by comparing with one entry 如何仅将新项目从一个列表复制到另一个 - How to copy only new items from one list to another 如何一步将一个值与所有列表条目进行比较? - How to Compare one value with all list entries in one step? 在列表中<Object> ,如何保留最近的条目并删除具有相同字段的先前条目? - In a List<Object>, how can I keep the most recent entry and remove previous entries which have an identical field? 列出 mongodb 中的所有文档,其中每个文档的一个字段在 spring 引导中具有最大值 - List all documents from mongodb where the each document's one field has max value in spring boot 映射条目的每个键的流,包含列表作为值 - Stream for each key of map entry, containing list as a value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM