<div class="blockcode"><blockquote>package conm.heima.homeworker;
import java.util.Comparator;
import java.util.TreeSet;
public class Test1 {
/*
* 1.分析以下需求,并用代码实现:
(1)定义一个员工类Employee,包含三个属性:姓名(String name)、年龄(int age)、工资(int salary)
(2)创建5个对象装入TreeSet,按照工资从高到底排序输出结果(工资相同,按照年龄从低到高,工资和年龄都相同,按照姓名字典顺序排列,
但是不能去重(姓名、年龄、工资都相同))
*/
public static void main(String[] args) {
TreeSet<Employee> ts = new TreeSet<>(new Comparator<Employee>() {
@Override
public int compare(Employee e1, Employee e2) {
int salary = e2.getSalary() - e1.getSalary();
int sage = e1.getAge() - e2.getAge();
int com = e1.getName().compareTo(e2.getName());
int name1= com == 0 ? 1: com;
return salary = sage == 0 ? name1 : salary;
}
});
ts.add(new Employee("zhangsan", 23,20000));
ts.add(new Employee("lisi", 24,30000));
ts.add(new Employee("lisi", 24,30000));
ts.add(new Employee("wangwu", 35,40000));
ts.add(new Employee("zhaoliu", 35,40000));
ts.add(new Employee("zhouqi", 36,80000));
ts.add(new Employee("qianba", 47,60000));
System.out.println(ts);
}
}
package conm.heima.homeworker;
//定义一个员工类Employee,包含三个属性:姓名(String name)、年龄(int age)、工资(int salary)
public class Employee {
private String name;
private int age ;
private int salary;
public Employee() {
super();
}
public Employee(String name, int age, int salary) {
super();
this.name = name;
this.age = age;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + ", salary=" + salary + "]";
}
}
</blockquote></div><br /> |
|