-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindAllPositiveNumbers.java
More file actions
54 lines (43 loc) · 1.55 KB
/
FindAllPositiveNumbers.java
File metadata and controls
54 lines (43 loc) · 1.55 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class FindAllPositiveNumbers
{
static int[] positiveArray;
static int[] negativeArray;
public static void findPositiveValues(int[] a)
{
List<Integer> list = new ArrayList<>();
Arrays.stream(a)
.forEach(v -> {
list.add(v);
});
positiveArray = new int[list.size()];
negativeArray = new int[list.size()];
System.out.println("list : " + list);
// usign method reference
positiveArray = list.stream()
.filter(v -> v > 0)
.mapToInt(Integer::intValue)
.sorted()
.toArray();
// usign just lambda expression
negativeArray = list.stream()
.filter(v -> v < 0)
.mapToInt(i -> i)
.sorted()
.toArray();
}
public static void main(String[] args)
{
int[] arr = {-2, 2, 10, 5, -5, 20, 1};
System.out.println("\nFind all Positive and Negative values: ");
findPositiveValues(arr);
System.out.print("positive int[] : ");
Arrays.stream(positiveArray)
.forEach(i -> System.out.print(i + " "));
System.out.print("\nnegative int[] : ");
Arrays.stream(negativeArray)
.forEach(i -> System.out.print(i + " "));
}
}