I'm trying to generate a stream of integers, so I made a static method that returns a Stream
.
Here is the method:
public static Stream<Integer> multipleOf3(){
return Stream.iterate(1, x -> 3*x).limit(10);
}
However this method don't return me the mutitples of three but the powers of three.
I call the method like this:
multipleDe3().forEach(System.out::println);
and I have this result:
1
3
9
27
81
243
729
2187
6561
19683
I think the iterate
function uses the previous result, the seeds = 1, so x = 1 and then:
3*1 = 3,
3*3 = 9,
3*9 = 27, etc...
If anyone has an idea to calculate the multiple without using the previous result tell me please.
public static Stream<Integer> multipleOf3(){
return Stream.iterate(0, x -> 3+x).skip(1).limit(10);
}
public static void main(String[] args) {
multipleOf3().forEach(System.out::println);
}
Output : 3 6 9 12 15 18 21 24 27 30