In this tutorial, we will see “How to find the second largest number in an array using Java 8?”
find second largest number in an array using java 8 stream
Java 8 – How to find the Second Largest Number in an Array? – Simplest Examples Share on X
Java 8 1st method:
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 | /** * Using Skip method of Java 8 Stream, Find the second largest number in an array * @author Deepak Verma * */ import java.util.Arrays; import java.util.stream.IntStream; public class Find_Second_Largest_Number_In_Array_Java8Stream_Example1 { public static void main(String args[]) { int[] numbers = {3,15,29,18,73,64}; // Get a stream of the numbers IntStream stream = Arrays.stream(numbers); int secondLargest = stream.sorted() .skip(numbers.length - 2) .findFirst() .getAsInt(); System.out.println("Second Largest Number in the Array is: "+secondLargest); } } |
Output:
1 | Second Largest Number in the Array is: 64 |
check if text or string present in a file using java 8
In above example, It first sorts the array, then skips the last element (the largest one) using skip(arr.length - 2)
, and finally finds the first element in the remaining stream, which is the second largest number.
Java 8 2nd method:
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 | /** * Using Skip and Limit method of Java 8 Stream, Find the second largest number in an array * @author Deepak Verma * */ import java.util.Arrays; import java.util.Comparator; import java.util.List; public class Find_Second_Largest_Number_In_Array_Java8Stream_Example2 { public static void main(String args[]) { List<Integer> numbers = Arrays.asList(12, 18, 15, 8, 31); int secondLargest = numbers.stream() .sorted(Comparator.reverseOrder()) .limit(2) .skip(1) .findFirst() .get(); System.out.println("Second Largest Number in the Array is: "+secondLargest); } } |
Output:
1 | Second Largest Number in the Array is: 18 |
find second largest number in an array using java 8 stream
In above example, It first sorts the array in descending order, then limits the stream to the first 2 elements using limit(2)
, then skip the first element using skip(1)
, and finally finds the first element in the remaining stream, which is the second largest number.
skip method java 8 stream
Java 8 – How to find the Second Largest Number in an Array? – Simplest Examples Share on X
Do you like this Post? – then check my other helpful posts:
- Convert a Stream to a List in Java 8
- Stream maptoint in Java 8 with examples
- Double the numbers of specified ArrayList using Streams
- Double the even / odd numbers of a specified ArrayList using Streams
- How to check if Number is Prime or not using Streams
- Retrieve Even/Odd Numbers within the Range using Java 8 Streams
- How to add/sum up the ArrayList integers using Java8 Streams
- Generate Prime Numbers in Java 8 using Streams
- Comparator example – Collections sort with/without Lambda in Java 8
- How to pass function as a parameter in a method in Java 8?
- Remove duplicates from ArrayList in Java 8
- ForEach examples for Map/List in Java 8
Other Useful References: