我正在研究一個虛擬醫院資料庫。我有一個ArrayList醫生理論上可以預約的所有可能時間的組合,以及另一個ArrayList持有實際注冊預約的時間。
Availability {
int doctorid;
String specialty;
Date date;
int order_of_appointment;
}
//////////
ArrayList<Availability> allTimes;
ArrayList<Availability> busyTimes;
我想要完成的是找到醫生有空的時間。這是 (allTimes - busyTimes) 的結果
我嘗試使用allTimes.removeAll(busyTimes)但它沒有洗掉任何內容。
我確保我覆寫了類中的 equals() 方法,Availability但它仍然沒有洗掉任何東西。
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Availability)) return false;
Availability that = (Availability) o;
return doctorid == that.doctorid &&
order_of_appointment == that.order_of_appointment &&
Objects.equals(specialty, that.specialty) &&
Objects.equals(date, that.date);
}
輸出:
busyTimes = [Availability {doctorid=1, special='internal Medicine', date=2021-11-02, order_of_appointment=2}]
allTimes = [Availability{doctorid=1, special='internal Medicine', date=2021-11-02, order_of_appointment=1} , Availability{doctorid=1, special='internal Medicine', date=2021-11-02, order_of_appointment=2} , 可用性{doctorid=1, special='internal Medicine', date=2021-11-02, order_of_appointment=3}]
我為freeTimes獲得的輸出與allTimes相同,即使我希望它洗掉 order_of_appointment==2 的約會。
我對可能導致這種情況的原因完全一無所知。任何幫助,將不勝感激。提前致謝!
uj5u.com熱心網友回復:
你沒有展示你是如何創建陣列的,或者你是如何添加元素的。
我用 ArrayLists 做了一個簡單的程式并按預期作業:
import java.util.*;
class A {
int id;
A(int id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
return o instanceof A && this.id == ((A)o).id;
}
public String toString() {
return String.format("A{id:%s}", id);
}
public static void main(String ... args) {
List<A> a = new ArrayList<A>(Arrays.asList(new A(1), new A(2)));
List<A> b = new ArrayList<A>(Arrays.asList(new A(2), new A(3)));
a.removeAll(b);
System.out.println(a);
}
}
輸出:
[A{id:1}]
uj5u.com熱心網友回復:
錯誤的是,即使我將日期格式設定為“yyyy-MM-dd”,它仍然將時間存盤在 Date 物件中。我將 Date 物件轉換為字串,然后比較這些字串并且有效。謝謝你們。
uj5u.com熱心網友回復:
您需要使用比較邏輯覆寫Availability 類中的equals(Object obj) 方法。
我實施了它。請檢查。
import java.util.Date;
public class Availability {
private int doctorId;
private String specialty;
private Date date;
private int orderOfAppointment;
public Availability() {
super();
}
public Availability(int doctorId, String specialty, Date date, int orderOfAppointment) {
super();
this.doctorId = doctorId;
this.specialty = specialty;
this.date = date;
this.orderOfAppointment = orderOfAppointment;
}
public int getDoctorId() {
return doctorId;
}
public void setDoctorId(int doctorId) {
this.doctorId = doctorId;
}
public String getSpecialty() {
return specialty;
}
public void setSpecialty(String specialty) {
this.specialty = specialty;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public int getOrderOfAppointment() {
return orderOfAppointment;
}
public void setOrderOfAppointment(int orderOfAppointment) {
this.orderOfAppointment = orderOfAppointment;
}
@Override
public String toString() {
return "Availability [doctorId=" doctorId ", specialty=" specialty ", date=" date
", orderOfAppointment=" orderOfAppointment "]";
}
@Override
public boolean equals(Object obj) {
boolean returnVal = false;
Availability busyslote = (Availability) obj;
if (this.doctorId == busyslote.doctorId && this.orderOfAppointment == busyslote.orderOfAppointment
&& this.specialty.equalsIgnoreCase(busyslote.specialty) && this.date.equals(busyslote.date)) {
returnVal = true;
} else {
returnVal = false;
}
return returnVal;
}
}
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class AppointmentMain {
public static void main(String[] args) {
List<Availability> allAppointment = new ArrayList<>();
List<Availability> attenedAppointment = new ArrayList<>();
Availability obj1 = new Availability(1, "Internal Medicine", new Date(), 1);
Availability obj2 = new Availability(1, "Internal Medicine", new Date(), 2);
Availability obj3 = new Availability(1, "Internal Medicine", new Date(), 3);
allAppointment.add(obj1);
allAppointment.add(obj2);
allAppointment.add(obj3);
Availability obj4 = new Availability(1, "Internal Medicine", new Date(), 3);
attenedAppointment.add(obj4);
System.out.println("Befour count :" allAppointment.size());
allAppointment.removeAll(attenedAppointment);
System.out.println("After count :" allAppointment.size());
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/347889.html
