三.分析以下需求,并用代码实现:
(1)定义一个员工类Employee,包含三个属性:姓名(String name)、年龄(int age)、工资(int salary)
(2)创建5个对象装入TreeSet,按照工资从高到底排序输出结果(工资相同,按照年龄从低到高,工资和年龄都相同,
按照姓名长度升序,但是不能去重(姓名、年龄、工资都相同))
我用Treeset写实在想不出来怎么搞?
public class test03 {
public static void main(String[] args) {
Employee e1 = new Employee("烬", 26, 6300);
Employee e2 = new Employee("小炮", 25, 3150);
Employee e3 = new Employee("伊泽瑞尔", 25, 4800);
Employee e4 = new Employee("vn", 25, 4800);
Employee e5 = new Employee("vn", 25, 4800);
TreeSet<Employee> set = new TreeSet<>(new Comparator<Employee>() {
@Override
public int compare(Employee o1, Employee o2) {
//创建5个对象装入TreeSet,按照工资从高到底排序输出结果(工资相同,按照年龄从低到高,工资和年龄都相同,
//按照姓名长度升序
int result1=o2.getSalary()-o1.getSalary();//按照工资从高到底排序输出结果
int result2=o1.getName().length()-o2.getName().length();
result1=result1 ==0?o1.getAge()-o2.getAge():result1;
result2=result2 ==0?o1.getName().length()-o2.getName().length():result1;//获取name长度
return result2;
}
});
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);
for (Employee employee : set) {
System.out.println(employee);
}
}
}