Is it possible to create objects with parameters while using Stream class? I'd like to reproduce the following with Java 8 Stream.
for(Integer id:someCollectionContainingTheIntegers){
someClass.getList().add(new Whatever(param1, id));
}
Sure. But if you have a collection, you can use forEach
and a lambda:
someCollectionContainingTheIntegers.forEach(id -> someClass.getList().add(new Whatever(param1, id));
Another possible variation is to collect into the destination list:
someCollectionContainingTheIntegers.stream()
.map(id -> new Whatever(param1, id))
.collect(Collectors.toCollection(() -> someClass.getList()));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With