这是我到目前为止为其他需要做的事情编写的代码。主要的问题是,即使有这么多的信息,我也无法理解该怎么做。
从服务器场列表中创建一个地图,该地图具有相同的薪水键,并具有相同的薪水。显示地图内容,并为每位员工显示正在工作的农场
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());
}
}
农场班
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);
}
public Employee getMostPaidEmployee() {
return Collections.max(emp);
}
}
而我的主要
public static void main(String args[])
{
List <Farm> FarmList = new ArrayList<Farm>();
Farm farm1 = new Farm("Tiguana" , 700);
farm1.makeList("Mihai", 30000);
farm1.makeList("Vladimir", 4000);
farm1.makeList("Tusnic", 3000);
farm1.getList();
System.out.println(farm1.getMostPaidEmployee());
Farm farm2 = new Farm("Tipirei" , 800);
farm2.makeList("Mihai", 30000);
farm2.makeList("Mihail", 40000);
farm2.makeList("Armando", 50000);
farm2.makeList("ASihai", 60000);
System.out.println(farm2.getMostPaidEmployee());
FarmList.add(farm2);
FarmList.add(farm1);
}
最佳答案
这就是你要怎么做:
Map<Integer, List<Employee>> map = new HashMap<>();
for(Farm f : FarmList){
for(Employee e : f.emp){
if(!map.containsKey(e.salary)){
map.put(e.salary, new ArrayList<>());
}
map.get(e.salary).add(e);
}
}
确保将FarmName字段添加到您的员工,并在Employee类中重写toString()方法,以便您可以显示要求的内容。