If you want to how to find nth highest salary of employees in Java? Create 10 object for the class Employee which will have name, salary and id and find the nth highest salary employee details.
To find Nth highest salary is a very common interview question if you are going for the role at junior level. Also understand how this sql query works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package com.employee; import java.util.ArrayList; import java.util.Collections; public class EmployeeNthSalary { /* Create 10 object for the class Employee which will have name, salary and id and find the nth highest salary employee details. */ public static void main(String args[]) { ArrayList<Employee> emp = new ArrayList<Employee>(); emp.add(new Employee("Niel", 50000, 1)); emp.add(new Employee("Arun", 20000, 2)); emp.add(new Employee("Jake", 80000, 3)); emp.add(new Employee("Ankit", 90000, 4)); emp.add(new Employee("Salil", 10000, 5)); emp.add(new Employee("Kunal", 100000, 6)); emp.add(new Employee("Sanjul", 50000, 7)); emp.add(new Employee("Pankaj", 70000, 8)); emp.add(new Employee("Prem", 75000, 9)); emp.add(new Employee("Dia", 95000, 10)); Collections.sort(emp); int nthHighestSal = 7; String employeeWith4thHighestSal = emp.get(nthHighestSal - 1).name + " " + emp.get(nthHighestSal - 1).salary + " " + emp.get(nthHighestSal - 1).id; System.out.println(employeeWith4thHighestSal); } } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.