How to Find Second Highest Value Using JAVA 8?

 Interview Question?

Interview Question Capgemini? Q. Write a Logic Second Highest Value ,take a Array take 5 or 6 elements and in that Array I want Second Highest Value?

Solution:-

package p3;

import java.util.Arrays;
import java.util.List;

public class SecondHighestValue {

public static void main(String[] args) {
Integer[] numbers= {10,50,40,30};
List<Integer> list=Arrays.asList(numbers);
Integer secondHighest=list.stream()
.distinct().sorted((a,b)->b-a).skip(1).findFirst().orElse(null);
System.out.println("The Second Highest Number is :"+secondHighest);

}

}

*************************************************************
O/P:-

The Second Highest Number is :40

Comments

Popular posts from this blog

What are POJO classes in spring? What is the use of POJO Class, and How to create POJO classes.

How to create React project using command prompt?