-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamDemo.java
More file actions
36 lines (28 loc) · 1.08 KB
/
Copy pathStreamDemo.java
File metadata and controls
36 lines (28 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package stream;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class StreamDemo {
public static void main(String[] args) {
List<Integer>list=Arrays.asList(10,11,45,79,9,6,5,3,6,8,9);
//list.stream().forEach((ele)->System.out.println(ele)); //using for each method print the stream
//Optional<Integer> optional=list.stream().findFirst();
//System.out.println(optional.get());
//list.stream().filter((num)->num%2==0).forEach((num->System.out.println(num)));
// List<Integer> odd=list.stream().filter((num)->num%2!=0).collect(Collectors.toList());
// System.out.println(odd);
//map method
//list.stream().map(a1->a1%2!=0?a1+1:a1).forEach(a1->System.out.println(a1));
//list.stream().filter((a1)->isPrime(a1)).forEach(a1->System.out.println(a1));
list.stream().sorted().forEach(a1->System.out.println(a1));
}
public static boolean isPrime(int num) {
for(int i=2;i<num;i++) {
if(num%i==0) {
return false;
}
}
return true;
}
}