<Employe> 的 TreeSet 列表对列表进行排序并显示列表中具有特定属性的人员

TreeSet list of <Employe> order the list and show people from list with specific properties

我有一个 Employee 列表(Employee 是 class)。员工有 3 个属性

private String name    
private int yearsSpentInCompany
private boolean parkingSpace

并具有这些属性的构造函数

 public Employee(String name, int yearsSpentInCompany, boolean parkingSpace) {
        this.name = name;
        this.yearsSpentInCompany = yearsSpentInCompany;
        this.parkingSpace = parkingSpace;}

名单是这个

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

所以我在列表中有 10 名员工。我如何获得没有任何停车位的员工列表 spaces (parkingSpace = false),以及根据在公司工作的年限排序的相同列表。

我不完整的解决方案:我定义了自己在公司工作多年的比较器

import java.util.Comparator;

public class YearsComp implements Comparator<Employee> {

    @Override
    public int compare(Employee o1, Employee o2) {
        if (o1.getYearsSpentInCompany() > o2.getYearsSpentInCompany()) {
            return 1;
        } else {
            return -1;
        }
    }
}

输出为

=== Employees sorted by seniority in work === 
Employee: Vlad === YearsSpentInCompany: 1 === ParkingPlace:true
Employee: Ayian === YearsSpentInCompany: 2 === ParkingPlace:true
Employee: Ion === YearsSpentInCompany: 2 === ParkingPlace:true
Employee: Victor === YearsSpentInCompany: 2 === ParkingPlace:false
Employee: Aurel === YearsSpentInCompany: 8 === ParkingPlace:false
Employee: Tudod === YearsSpentInCompany: 10 === ParkingPlace:true
Employee: Sebi === YearsSpentInCompany: 13 === ParkingPlace:true
Employee: Marcel === YearsSpentInCompany: 15 === ParkingPlace:false
Employee: Raul === YearsSpentInCompany: 16 === ParkingPlace:true
Employee: Andrei === YearsSpentInCompany: 17 === ParkingPlace:false

排序不错。 Emplyee class 在此 class:

中实现 Comparable 和 i Ovveride
@Override
public int compareTo(Object o) {
        if( this.parkingSpace == false)
            System.out.println("No parking place: " + this.getName());
        return 0;
    }

输出为

No parking place: Marcel  ---15 years
No parking place: Aurel   ---- 8 years
No parking place: Victor   ---- 2 years
No parking place: Andrei    --- 17 years

这是部分正确的,因为我想对没有停车位的员工进行排序 space 按照他们在这样的公司工作的年限

No parking place: Victor   ---- 2 years
No parking place: Aurel   ---- 8 years
No parking place: Marcel  ---15 years
No parking place: Andrei    --- 17 years

我如何结合这两种方法?

使用compareTo让员工没有停车场是没有意义的。 compareTo 应该用于排序(以及 TreeSet 中的唯一性测试),而不是用于过滤。

您可以使用 Java 8 Streams:

轻松筛选和排序
List<Employee> output =
    allEmployeesOfCompany.stream()
                         .filter(emp -> !emp.parkingSpace)
                         .sorted(Comparator.comparingInt(Employee::getYearsSpentInCompany))
                         .collect(Collectors.toList()); 

不使用 Streams 的解决方案:

    List<Employee> filteredList = new ArrayList<>();
    for(Employee e : allEmployeesOfCompany) {
        if(!e.parkingSpace)
            filteredList.add(e);
    }

    Collections.sort(filteredList, new YearsComp());

使用 Streams 的解决方案:

    YearsComp yearsComparator = new YearsComp();
    List<Employee> filteredAndSortedList = allEmployeesOfCompany.stream()
            .filter(e -> !e.parkingSpace)
            .sorted(yearsComparator)
            .collect(Collectors.toList());

旁注:

如果 2 名员工的年数相同,您的比较方法应该 return0。如果在 YearsComp 中进行比较,则无需实现 Comparable。

public class YearsComp implements Comparator<Employee> {

    @Override
    public int compare(Employee o1, Employee o2) {
        int returnValue = 0;
        if (o1.getYearsSpentInCompany() > o2.getYearsSpentInCompany()) {
            returnValue = 1;
        } else if (o1.getYearsSpentInCompany() < o2.getYearsSpentInCompany()) {
            returnValue = -1;
        }
        return returnValue;
    }
}

我认为在这种情况下最好有一个单独的 Comparator 而不要实现 Comparable,因为如果你以后需要不同的比较,你可以创建另一个 Comparator,但是如果你继续实现 Comparable 那么你只能有1个比较。