I need to get the max from a Employee class from a Farm class

I need to find and display the the employee who has the maximum salary from the Farm. this is what I got so far

public class Employee implements Comparable<Employee> {
private String name;
private Integer salary;

public Employee (String name , Integer salary) {
    this.name = name;
    this.salary = salary;
}
public void setName(String name) {
    this.name = name;
}
public String getName() {
    return name;
}
public void setSalary(Integer salary) {
    this.salary = salary;
}
public Integer getSalary() {
    return salary;
}
public String toString() {
    return name + " " + salary;
}
public int compareTo(Employee emp) {
    return this.salary.compareTo(emp.getSalary());
}

}

Employee class

public class Farm {
private String name;
private Integer surface;

List<Employee> emp = new ArrayList<Employee>();


public Farm(String name , Integer surface) {
    this.name =  name;
    this.surface = surface;
}
public void setName(String name) {
    this.name = name;

}
public String getName() {
    return name;
}
public void setSurface(Integer surface) {
    this.surface = surface;
}
public int getSurface () {
    return surface;
}
public String toString() {
    return name + " " + surface;
}
public void makeList(String ename , Integer esalary) {
    this.emp.add(new Employee(ename,esalary));
}
public void getList() {
    for(Employee el : emp)
        System.out.println(el);
}

}

And the last one is the main. The thing is that I don't know how can I have more farms and get the max from every single one of them. Can you guys help me?

And this is my mainapp

public class Mainapp {
public static void main(String args[])
{
    List <Farm> FarmList = new ArrayList<Farm>();
    FarmList.add(new Farm("unirea pizdii", 890030));
    FarmList.add(new Farm("pseudo autsm",78594));
    FarmList.add(new Farm("haha hihi",854856099));

    Farm farm1 = new Farm("Tiguana" , 700);
    farm1.makeList("Mihai", 30000);
    farm1.makeList("Vladimir", 4000);
    farm1.makeList("Tusnic", 3000);
    farm1.getList();


    Employee emp1 =  new Employee(" mihai", 3000);
    System.out.println(emp1);
}

}

Answer:1

To get employee with max salary for each farm you can use stream api:

import static java.util.stream.Collectors.*;

Map<Farm, Optional<Employee>> collect = 
        farmList.stream().collect(groupingBy(Function.identity(),
            flatMapping(farm -> farm.getEmployes().stream(), 
                   maxBy(Employee::compareTo))));

Result map has Farm as a key and Employee with max salary as a value

Note: flatMapping method is from java9

Answer:2

I am trying to build a java server using AWS on an EC2 server on tomact. Why i am building is for a game i am making in unity. This will not be a real time game where I need a constant connection ...

I want use the android NDK to make my c++ engine work on android without having to rewrite the engine in java. Can system calls for android, for example touch input, accelerometer, windowing and all ...

I am creating a SpannerSingleton to stay connected for the duration of the app's life. I'm interested in connection durability... if there is a session/connection issue how can I recreate a session? ...

First, I am a new to Spring and this is my first attempt to write a REST based application using Spring. I am planning to use Json both request parameters and in responses. This brings me to two ...