javaarraysclass

how to return a list of employee's names with the highest pay rate in java


Assume i have a valid Employee class. I create a PayRoll class with main method. `

public class PayRoll {

    public static void main(String[] args) {

        //create 6 Employee objects Employee(String name, String id, int hours, double rate)
        Employee e1 = new Employee("Melissa Black", "M7620", 30, 50.00);
        Employee e2 = new Employee("Richard Hamerly", "R2304", 45, 35.00);
        Employee e3 = new Employee("Bruce Beverly", "B9201", 25, 30.00);
        Employee e4 = new Employee("Jessica Khan", "K7248", 42, 60.00);
        Employee e5 = new Employee("Tom Sylvester", "S3810", 40, 45.00);
        Employee e6 = new Employee("Ariana Bailey", "B4092", 40, 60.00);

        //create an Employee array and add the objects above
        Employee[] list = {e1, e2, e3, e4, e5, e6};
        System.out.print(highRateEmployee(list));

         }
 //method that returns the name of the employee with the highest payrate
    public static String highRateEmployee(Employee [] list) {
        double max = list[0].getPayRate();
        String name = list[0].getName();
        for(int i=1; i<list.length; i++) {
            if(list[i].getPayRate() > max) {
                max = list[i].getPayRate();
                name = list[i].getName();
            }
        }
        return name;
}

I create a function that return only the first employee with the highest payrate. Output of the following function is Jessica Khan.

//method that returns the name of the employee with the highest payrate
    public static String highRateEmployee(Employee [] list) {
        double max = list[0].getPayRate();
        String name = list[0].getName();
        for(int i=1; i<list.length; i++) {
            if(list[i].getPayRate() > max) {
                max = list[i].getPayRate();
                name = list[i].getName();
            }

        }
        return name;
    }

How can I make a function that return a list of employee's names with the highest pay? Here's my attempt. I tried creating an arraylist name instead of name and use .add() method. However, it appears error that "Cannot invoke add(String) on the array type String[]" when I'm trying to write "name.add(list[i].getName());"

    public static String[] highRateEmployee(Employee [] list) {
        double max = list[0].getPayRate();
        String[] name = {};
        for(int i=1; i<list.length; i++) {
            if(list[i].getPayRate() >= max) {
                max = list[i].getPayRate();
                name.add(list[i].getName());
            }

        }
        return name;
    }

Solution

  • As suggested, you should use an array list so you can add the names when you find them. However, your method as written even using lists won't work.

    That is because you are adjusting max as you iterate across the employee list. So if the first value of max is 35 and you find one greater, you will add it to the list. Doing it this way you will be adding names who have rates less than the max. In fact, you are omitting the first employee entirely from the test regardless.

    To solve this I recommend using a Map<Double, List<String>> See HashMap

    public static List<String> highRateEmployee(List<Employee> employees) {
    
            Map<Double, List<String>> names = new HashMap<>();
            double max = 0;
            for (Employee emp : employees) {
                names.computeIfAbsent(emp.getPayRate(), k -> new ArrayList<>())
                        .add(emp.getName);
                max = Math.max(max, emp.getPayRate());
            }
            return names.get(max);
    }
    

    The returned list of names would be

    [Jessica Khan, Ariana Bailey]