-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFindingMaxInJava8.java
45 lines (36 loc) · 1.51 KB
/
FindingMaxInJava8.java
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
package com.javaexperiments;
import java.util.Arrays;
/**
* Demonstrates finding the maximum and minimum values in an array using
* both traditional and Java 8 Stream API approaches.
*/
public class FindingMaxInJava8 {
public static void main(String[] args) {
int arr[] = {61, 54, 23, 5, 23};
/*
* Using Traditional Approach
* - Iterates through the array to find the maximum and minimum values manually.
*/
int maxim = arr[0]; // Initialize maximum with the first element
int minim = arr[0]; // Initialize minimum with the first element
for (int i = 1; i < arr.length; i++) {
if (arr[i] > maxim) { // If current element is greater than the maximum
maxim = arr[i];
} else if (arr[i] < minim) { // If current element is smaller than the minimum
minim = arr[i];
}
}
System.out.println("Maximum number (Without Stream) is " + maxim);
System.out.println("Minimum number (Without Stream) is " + minim);
/*
* Using Java 8 Stream API
* - Streams provide a functional approach to process data.
*/
// Find the maximum value using stream
int max = Arrays.stream(arr).max().getAsInt();
// Find the minimum value using stream
int min = Arrays.stream(arr).min().getAsInt();
System.out.println("Maximum number (With Stream) is " + max);
System.out.println("Minimum number (With Stream) is " + min);
}
}