¿Cómo encontrar el salario mínimo de la lista de empleados en Java?

Hi so I have two classes Employee and Department. My main function reads off a .txt file filled with employee names, salaries, departments, and positions. My Employee class is just getters and setters. An list of arraylists are made to represent the employees and I'm not sure how I would find the minimum salary of each of the departments. To find the maximum salary I did this in my department class.

public class Department {
    String dept;
    List<Employee> employees;
    double minSal;
    double maxSal;

    public void addEmployee(Employee emp){
        maxSal = 0.0;
        if (maxSal < emp.getSalary()) {
            maxSal = emp.getSalary();
        }

but I'm not sure how to get the minimum salary. My idea was to get the salary of one of the employees from each department and use that as a starting point for

if (minSal > emp.getSalary()) {
    minSalary = emp.getSalary();
}

But I realized I had no idea what to do. Can I get some help with this?

preguntado el 25 de abril de 13 a las 02:04

Why not use a for loop to iterate through the ArrayList checking for the lowest salary as you loop? -

I saw that it was asked before but I didn't really understand how to use it for my case and I'm not sure how to use a loop to iterate through the arraylist. I just started java recently. -

You should go to the Java tutorials and read up on use of for loops. One skill that you will need to master to learn Java is how to learn things on your own. The Java tutorials are a maravillosa resource for this. Google can help you find it. -

5 Respuestas

if (employees.isEmpty() {
   return null; // No minimum salary available.
}
int minSalary = Integer.MAX_INT;
for (Employee e : employees) {
    minSalary = Math.min(minSalary, e.getSalary();
}
return minSalary;

Respondido 25 Abr '13, 02:04

It sounds like you want a list of the lowest salaries for each department and it looks like the other answers just give you the lowest salary across departments. If I'm correct and you want the low salary by department you probably want to just loop through the list and put them in a map by dept, like this:

public Map<String, Double> getLowSalaries(List<Employee> employees) {
    Map<String, Double> lowSalaries = new HashMap<String, Double>();

    for(Employee employee : employees) {
        Double lowSalaryForDept = lowSalaries.get(employee.getDept());

        if(lowSalaryForDept == null || lowSalaryForDept < employee.getSalary()) {
            lowSalaries.put(employee.getDept(), employee.getSalary());
        }
    }
    return lowSalaries;
}

Respondido 25 Abr '13, 03:04

There is a special number, Double.POSITIVE_INFINITY, which is greater than any number representable with double. You can use it as a starting point for searches of a minimum:

double minSalary = Double.POSITIVE_INFINITY;
...
if (minSal > emp.getSalary()) {
    minSalary = emp.getSalary();
}

Another common trick is to set minSalary to the first element of a list, and then start searching from the second element.

Respondido 25 Abr '13, 02:04

How do I go about setting it to the first element of a list? This was how I was thinking about it but I didn't know to do it. - user1869703

@user1869703 You can do it in a loop, not in addEmployee. Sin embargo, la POSITIVE_INFINITY trick should work. You can also use Double en lugar de double, in which case you can initially set minSal a null. - Serguéi Kalinichenko

Here's a variation using an Iterator:

public double minSalary(List<Employee> employees) {
    if (employees == null || employees.isEmpty()) {
        throw new IllegalArgumentException();
    }

    Iterator<Employee> iterator = employees.iterator();
    double min = iterator.next();
    while (iterator.hasNext()) {
        min = Math.min(min, iterator.next().getSalary());
    }

    return min;
}

Respondido 25 Abr '13, 03:04

primitive types cannot be null. It would probably be better to return Double.NaN or -1. - Acercándose Oscuridad

You're right, I updated it to throw an exception but your suggestions are good as well. Returning a Double would work also and then a null can be returned. - Ed Plese

  List<Employee> empArr=new ArrayList<>();
        empArr.add(new Employee("John", 1,100000));
        empArr.add(new Employee("Tom", 2,560000));
        empArr.add(new Employee("Sam", 3,4500));
        empArr.add(new Employee("David", 4,40000));

     int minSalary=   empArr.stream().map(e->e.getSalary()).min(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o1.compareTo(o2);
            }
        }).get();

        System.out.println(minSalary);

Respondido 29 Jul 22, 20:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.