I have a list of some Object called list. 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. I know how to do this using list.stream() twice but I'd like to do it under 1 stream.
This is how I've accomplished the task using list.stream() twice
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):
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
; and the unnecessary collect
for the second:
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:
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:
[a, a1, b, b1, c, c1]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.