Consider i have below list
List<Integer> alist = new ArrayList<>();
list=Arrays.asList(0,1,2,0,4,10);
alist.addAll(list);
I want the output as 0 0 1 2 0 0.
That means if there is a zero in the list add one more zero in list. Do this untill the size of list is same as input size.
How can this be done using Java 8 stream api?
You can achieve this by doing the following:
alist.stream()
.flatMap(i -> i == 0 ? Stream.of(i, 0) : Stream.of(i))
.limit(alist.size())
.collect(Collectors.toList());
This basically: