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

How to create React project using command prompt?

How to achieve abstraction?

Interview Question? How to find square of each element in this Array {2,4,5,6,8} using map ?