I want to create a sequence of integers, preferably as an IntStream, which follows a certain condition. some simple examples to explain what I am trying to do:
Sequence of first 10 even numbers starting from 0 (Here i have no problems, I do it with the below snippet)
IntStream.iterate(0, i -> i + 2).limit(10);
//0,2,4,6,8,10,12,14,16,18
Sequence of first 10 numbers starting from 0 by alternately adding 2 and 3 to the privious number; desired output:
//0,2,5,7,10,12,15,17,20,22
I would love to use for this scenario also IntStream.iterate() or IntStream.generate() but couldn't make it on my own. I am using classic for loops, which works but is somehow long for such a relative simple task
List<Integer> list = new ArrayList<>();
list.add(0);
for(int i = 1, j = 1; i < 10; i++, j *= -1){
if(j > 0){
list.add(list.get(i-1) + 2);
}
else{
list.add(list.get(i-1) + 3);
}
}
//and then stream over the list to get IntStream
list.stream().mapToInt(Integer::intValue);
Any easy way to achieve the same as above with IntStream.iterate() or IntStream.generate()?
For three and more alternating values I am out of ideas how to do it elegantly. If for example I want to create the Sequence of first 10 numbers starting from 0 by alternately adding +2, +5 and +7 to the previous number with the desired output:
//0,2,7,14,16,21,28,30,35,42
I have thought about using i%3 in a for loop with 3 if-else blocks or switch cases but this will grow when i need more alternating values and i have to add more ifs or cases. Any ideas how to do it? I am open for other approaches too if you think IntStream.iterate() or IntStream.generate() is not the proper way of solving the described task.