1. Overview
In this article, you'll learn how to find the largest value from an array. You should know the basic understanding of java Arrays, for-each loop, and java 8 lambdas.
Finding the biggest value can be done in different ways.
Let us explore 3 different ways using for each loop, sort() method, java 8 streams.
Note: Array is not sorted. if is sorted in ascending already then take the value from the array the last index-1 which gives the biggest value. If sorted descending, take zero index value.
2. Example 1 to find the largest value using for loop
This is a simple and iterative approach. Every developer writes this code easily in the interviews.
package com.javaprogramto.programs.arrays.largest;
import java.util.Arrays;
public class ForLooplargest {
public static void main(String[] args) {
// Creating unsorted array 1
int[] arr = { 15, 3, 67, 8, 20 };
System.out.println("Input array 1: " + Arrays.toString(arr));
// calling a method to get the large value.
int largest = getLargestNumber(arr);
System.out.println("largest value : " + largest);
// Creating unsorted array 1
int[] arr2 = { 900, -100, 500, 15000, 8377 };
System.out.println("Input array 2: " + Arrays.toString(arr2));
// calling a method to get the large value.
largest = getLargestNumber(arr2);
System.out.println("largest value : " + largest);
}
private static int getLargestNumber(int[] arr) {
int max = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
}
Output:
Input array 1: [15, 3, 67, 8, 20]
largest value : 67
Input array 2: [900, -100, 500, 15000, 8377]
largest value : 15000
3. Example 2 to find the largest value using Java 8 Streams
Arrays class is added with a new method stream() in java 8. Once the stream is created then next use the max() terminal method which returns Optional value. Finally, call getAsInt() method to get the largest. value as int type.
package com.javaprogramto.programs.arrays.largest; import java.util.Arrays; public class Java8StreamLargest { public static void main(String[] args) { // Creating unsorted array 1 int[] arr = { 15, 3, 67, 8, 20 }; System.out.println("Input array 1: " + Arrays.toString(arr)); // using java 8 stream(), max() methods. int largest = Arrays.stream(arr).max().getAsInt(); System.out.println("largest value : " + largest); // Creating unsorted array 1 int[] arr2 = { 900, -100, 500, 15000, 8377 }; System.out.println("Input array 2: " + Arrays.toString(arr2)); // using java 8 stream(), max() and getAsInt() methods largest = Arrays.stream(arr2).max().getAsInt(); System.out.println("largest value : " + largest); } }
Output:
This program produces the output as same as in the above section.
Input array 1: [15, 3, 67, 8, 20]
largest value : 67
Input array 2: [900, -100, 500, 15000, 8377]
largest value : 15000
4. Example 3 to get the largest number using Sorting(Arrays.sort)
In this technique, we are going to use the sort logic from java api using the Arrays.sort() method.
Once the array sorted, get the last value from it.
package com.javaprogramto.programs.arrays.largest;
import java.util.Arrays;
public class JavaSortLargest {
public static void main(String[] args) {
// Creating unsorted array 1
int[] arr = { 15, 3, 67, 8, 20 };
System.out.println("Input array 1: " + Arrays.toString(arr));
// using arrays.sort() method.
int largest = sortLargest(arr);
System.out.println("largest value : " + largest);
// Creating unsorted array 1
int[] arr2 = { 900, -100, 500, 15000, 8377 };
System.out.println("Input array 2: " + Arrays.toString(arr2));
// using arrays.sort() method.a
largest = sortLargest(arr2);
System.out.println("largest value : " + largest);
}
private static int sortLargest(int[] arr) {
Arrays.sort(arr);
return arr[arr.length - 1];
}
}
This apprach also produces the same output.
5. Conclusion
In this article, you've seen how to get the largest number from the array with different approaches.
All produce the same output but the java 8 approach has less code.
As usual, all examples are over GitHub.
No comments:
Post a Comment
Please do not add any spam links in the comments section.