Find nth Largest elements from Integer List using Java Stream


To find the nth highest elements from an integer list using Java 8 Stream API, you can follow these steps:

  1. Sort the integer list in descending order.
  2. Take the first n elements from the sorted list.

1. Find Single Element

public static Integer findNthHighestElement(List<Integer> integerList, int n) {

        if (integerList.isEmpty() || n <= 0 || integerList.size() < n)
            throw new IllegalArgumentException("Please validate your inputs.");

        return integerList.stream()
                .distinct()
                .sorted(Collections.reverseOrder())
                .collect(Collectors.toList()).get(n - 1);
    }

2. Find All Elements

To find all occurrences of the third highest elements in the list, you can modify the code to keep track of the third highest element and then filter all elements that are equal to it.

public static List<Integer> findAllNthHighestElements(List<Integer> integerList, int n) {

        if (integerList.isEmpty() || n <= 0 || integerList.size() < n)
            throw new IllegalArgumentException("Please validate your inputs.");

        Integer highestElement = integerList.stream()
                .distinct()
                .sorted(Collections.reverseOrder())
                .collect(Collectors.toList()).get(n - 1);

        return integerList.stream()
                .filter(e -> e == highestElement)
                .collect(Collectors.toList());
    }

Here is the complete example.

FindNthElements.java
package org.websparrow;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class FindNthElements {

    public static void main(String[] args) {

        List<Integer> integerList = Arrays.asList(10, 20, 5, 15, 30, 25, 20);
        int n = 3; // Change n to get the nth highest elements

        Integer highestElement = findNthHighestElement(integerList, n);
        System.out.println("The " + n + " highest element is: " + highestElement);

        List<Integer> allHighestElements = findAllNthHighestElements(integerList, n);
        System.out.println("The " + n + " highest elements are: " + allHighestElements);
    }

    public static Integer findNthHighestElement(List<Integer> integerList, int n) {

        if (integerList.isEmpty() || n <= 0 || integerList.size() < n)
            throw new IllegalArgumentException("Please validate your inputs.");

        return integerList.stream()
                .distinct()
                .sorted(Collections.reverseOrder())
                .collect(Collectors.toList()).get(n - 1);
    }

    public static List<Integer> findAllNthHighestElements(List<Integer> integerList, int n) {

        if (integerList.isEmpty() || n <= 0 || integerList.size() < n)
            throw new IllegalArgumentException("Please validate your inputs.");

        Integer highestElement = integerList.stream()
                .distinct()
                .sorted(Collections.reverseOrder())
                .collect(Collectors.toList()).get(n - 1);

        return integerList.stream()
                .filter(e -> e == highestElement)
                .collect(Collectors.toList());
    }
}

Output:

console
The 3 highest element is: 20
The 3 highest elements are: [20, 20]

References

  1. Java 8- Find the nth Highest Salary
  2. Java 8 – Find Non Duplicate Elements from List

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.