[Solved] Copy All the elements/object of one list N times to new list. Using java 8 stream


Streams might not be the right tool for this task. Why not just add all elments from original list to new list and calculate how many new entries to add after finding the largest id? Something like:

List<Fruit> newFruits =  new ArrayList<>();
newFruits.addAll(fruits);

int N = 10;
int toAdd = N - fruits.size();
int maxId = fruits.stream().mapToInt(Fruit::getId).max().orElse(0);

IntStream.rangeClosed(1,toAdd).forEach(i -> newFruits.add(new Fruit(maxId + i, "")));

Or take a look at @Holger’s comment.

1

solved Copy All the elements/object of one list N times to new list. Using java 8 stream