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:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester. He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

close

Enjoy this blog? Please spread the word :)

Follow by Email
Twitter