Nth highest salary from a given list using Java 8 Streams. Below is a step-by-step program that demonstrates this using distinct sorting and skipping elements.
Java Program: Find the Nth Highest Salary
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class NthHighestSalary {
public static void main(String[] args) {
List<Integer> salaries = Arrays.asList(50000, 60000, 75000, 40000, 75000, 90000, 60000);
int n = 4; // Change this value to find the nth highest salary
Optional<Integer> nthHighest = salaries.stream()
.distinct() // Remove duplicate salaries
.sorted((a, b) -> Integer.compare(b, a)) // Sort salaries in descending order
.skip(n - 1) // Skip (n-1) highest salaries
.findFirst(); // Get the nth highest salary
System.out.println(n + "th highest salary: " + nthHighest.orElse(null));
}
}
Exaplnation
Create a list of salaries: Some salaries may be duplicate.
Use .stream(): Convert the list into a stream.
Remove duplicates: .distinct() ensures only unique salaries are considered.
Sort in descending order: .sorted((a, b) -> Integer.compare(b, a)) arranges salaries from highest to lowest.
Skip (n-1) highest salaries: .skip(n - 1) moves past the first (n-1) elements.
Get the next element: .findFirst() extracts the Nth highest salary.
Print the result: If the salary exists, it prints, otherwise null is returned
Second highest salary with given list
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class SecondHighestNumber {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(12, 5, 8, 20, 15, 9, 20);
Optional<Integer> secondHighest = numbers.stream()
.distinct() // Remove duplicate numbers
.sorted((a, b) -> Integer.compare(b, a)) // Sort in descending order
.skip(1) // Skip the highest number
.findFirst(); // Get the second highest
System.out.println("Second highest number: " + secondHighest.orElse(null));
}
}
2 )
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
public class SecondHighestNumber {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(12, 5, 8, 20, 15, 9, 20);
Optional<Integer> highest = numbers.stream().max(Integer::compare);
Optional<Integer> secondHighest = numbers.stream()
.filter(num -> !num.equals(highest.orElse(null))) // Remove the highest
.max(Integer::compare); // Find the second highest
System.out.println("Second highest number: " + secondHighest.orElse(null));
}
}
3 .
public class SecondHighestSalaryWithSet {
public static void main(String[] args) {
List<Employee> employees = Arrays.asList(
new Employee("Alice", 50000),
new Employee("Bob", 60000),
new Employee("Charlie", 70000),
new Employee("David", 80000),
new Employee("Eve", 90000),
new Employee("Frank", 90000) // Duplicate salary
);
// Find the second highest salary using a Set to eliminate duplicates
Optional<Double> secondHighestSalary = employees.stream()
.map(emp -> emp.salary)
.collect(Collectors.toSet()) // Collect to a Set to remove duplicates
.stream()
.sorted(Comparator.reverseOrder())
.skip(1)
.findFirst();
// Print the result
System.out.println("Second Highest Salary: " + secondHighestSalary.orElse(-1));
}
}
No comments:
Post a Comment