I do not want create new list from Stream.collect method in second time. It should add in previous list only. I am trying to do like below but it is not working. As per document, Collectors.toList() is always create new list, and collect() method is not allowing old list. Is there any way i can reuse my previous list(listA) in second time.
List<A> listA = xList.stream().map().collect(Collectors.toList());
yList.stream().map().collect(**listA**);
Instead of using .collect
, write .forEach(listA::add)
.
(Technically, you should also replace Collectors.toList()
in the first example with Collectors.toCollection(ArrayList::new)
, because toList()
does not guarantee that it returns a mutable list.)