File tree 2 files changed +38
-0
lines changed
2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change
1
+ package jl004_streamprojects ;
2
+
3
+ import java .util .stream .IntStream ;
4
+
5
+ public class JavaStreamsAPI003 {
6
+ public static void main (String [] args ) {
7
+ // Create an infinite IntStream starting from 1 and limit it to the first 10 elements
8
+ IntStream stream = IntStream .iterate (1 , i -> i + 1 ).limit (10 );
9
+ // Print each element to the console
10
+ stream .forEach (System .out ::println );
11
+
12
+ // Create an infinite IntStream starting from 1 and limit it to the first 10 elements
13
+ // Square each element and print to the console
14
+ IntStream stream2 = IntStream .iterate (1 , i -> i + 1 ).limit (10 ).map (x -> x * x );
15
+ stream2 .forEach (System .out ::println );
16
+
17
+ // Create an infinite IntStream starting from 1 and limit it to the first 10 elements
18
+ // Square each element, filter only the even ones, and print to the console
19
+ IntStream stream3 = IntStream .iterate (1 , i -> i + 1 ).limit (10 ).map (x -> x * x ).filter (x -> x % 2 == 0 );
20
+ stream3 .forEach (System .out ::println );
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ package jl004_streamprojects ;
2
+
3
+ import java .util .Random ;
4
+ import java .util .stream .IntStream ;
5
+
6
+ public class JavaStreamsAPI004 {
7
+ public static void main (String [] args ) {
8
+ // Generate an infinite IntStream with random integers less than 100, and limit it to the first 10 elements
9
+ IntStream stream = IntStream .generate (() -> new Random ().nextInt (100 ));
10
+ stream .limit (10 ).forEach (System .out ::println );
11
+
12
+ // Generate an infinite IntStream with random integers less than 100, and limit it to the first 10 elements
13
+ IntStream stream2 = IntStream .generate (() -> new Random ().nextInt (100 )).limit (10 );
14
+ stream2 .forEach (System .out ::println );
15
+ }
16
+ }
You can’t perform that action at this time.
0 commit comments