有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java按日期和时间对ArrayList的对象进行排序

我在ArrayList中有10个对象,其中患者ID和预约日期作为实例变量。如何按预约日期对患者进行分类

由于某些原因,我无法使用ComparatorArrayList进行排序;编译器给出了一个错误:

java.util.Comparator is abstract; cannot be instantiated

当不使用ComparatorCollections.sort()Object不起作用

ArrayList<Appointment> testApp = new ArrayList<>();

while (!App.isEmpty()) {
    tempA = (Appointment) App.dequeue();

    if (tempA.getPatID().equals(search)) {
        testApp.add(tempA);
    }

    tempApp.enqueue(tempA);
}

while (!tempApp.isEmpty()) {
    tempA = (Appointment) tempApp.dequeue();
    App.enqueue(tempA);
}

Collections.sort(testApp, new Comparator<Appointment>());

共 (4) 个答案

  1. # 1 楼答案

    您不能直接实例化Comparator,您需要自己实现逻辑,请列出

        Comparator<Appointment> comp = new Comparator<>() {
            public int compare(Appointment a1, Appointment a2) {
                // Logic to compare here, see javadoc of that method
                return 0;
            }
        }
    
        testApp.sort(comp);
    

    使用lambda表达式,可以编写如下代码:

        testApp.sort((a1, a2) -> {
            // Logic to compare here, see javadoc of that method
            return 0;
        });
    

    如果Appointment是您自己编写的类,则可以使其实现Comparable<Appointment>

    class Appointment implements Comparable<Appointment> {
            public int compareTo(Appointment a2) {
                // Logic to compare here, see javadoc of that method
                return 0;
            }
    }
    

    这意味着您可以在不指定比较器的情况下对列表进行排序:

        testApp.sort(null);
    
  2. # 2 楼答案

    我假设您的Appointment类如下所示:

    class Appointment {
        private int patientId;
        private LocalDateTime appointmentDate;
    
        // Getters & setters
    }
    

    如果是这样,您应该创建一个Comparator来使用^{}方法进行排序:

    ArrayList<Appointment> appointments = new ArrayList<>();
    
    Collections.sort(appointments, Comparator.comparing(appointment -> {
        return appointment.getAppointmentDate();
    }));
    

    lambda函数告诉比较器如何获取对象(一个Appointment)并提取用于排序的元素(键;appointmentDate)。如果您愿意,可以将其浓缩为方法引用:

    Collections.sort(appointments, Comparator.comparing(Appointment::getAppointmentDate));
    

    您也可以直接在ArrayList上调用^{}

    appointments.sort(Comparator.comparing(Appointment::getAppointmentDate));
    

    如果您总是(或通常)按日期排序您的^ {< CD1>}对象,则可以考虑使^ {CD1>}实现^{},这样您就可以调用^ {< CD11>}而不传递^ {< CD2>}:

    class Appointment implements Comparable<Appointment> {
        private int patientId;
        private LocalDateTime appointmentDate;
    
        // Getters & setters
    
        @Override
        public int compareTo(final Appointment other) {
            return appointmentDate.compareTo(other.appointmentDate);
        }
    }
    
  3. # 3 楼答案

    private static final Comparator<Appointment> SORT_BY_DATE_DESC = Comparator.comparing(Appointment::getDate).reversed();
    
    public static void main(String... args) {
        List<Appointment> appointments = Collections.emptyList();
        appointments.sort(SORT_BY_DATE_DESC);
    }
    
    public static class Appointment {
        private Date date;
    
        public Date getDate() {
            return date;
        }
    }
    
  4. # 4 楼答案

    为了使您的类实例彼此具有可比性,您的类大多数实现“可比”接口。例如,如果您有一个名为C1的类,并且您希望使C1实例具有可比性,则大多数情况下会以以下方式声明C1:

    public class C1 implements Comparable<C1>{
    //some codes
    
    @Override
    public int compareTo(C1 other){
    // here you can compare this instance of C1 (references with this keyword) with other
    
    }
    
    }
    

    现在,如果您使用Collections.sort(),您的arrayList将被排序