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 Click To Tweet

Java 8 1st method:

 

Output:

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:

 

Output:

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 Click To Tweet

Do you like this Post? – then check my other helpful posts:

Other Useful References: