如何编写if-else语句判断ArrayList中Appointment对象多属性匹配
判断医生指定日期时段是否已有预约
要判断指定医生在某日期和时段是否已有预约,你可以通过遍历ArrayList<Appointment>,逐一比对对象的三个核心属性(医生执照号、日期、时段)来实现。以下是具体实现方式:
注意事项
Date类的equals()方法会精确比对毫秒值,如果你只需要比对年月日(忽略时分秒),需要额外处理日期部分,否则直接用Date的equals()可能会因为毫秒差异导致判断错误。- 时段字符串的比对要注意大小写,比如示例中的
"9 AM - 10 AM"和"9 AM - 10 Am",如果需要忽略大小写可以用equalsIgnoreCase(),否则用equals()严格匹配。
实现代码
方式1:传统for循环 + if-else判断
import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Date; public class AppointmentChecker { public static void main(String[] args) { // 示例预约列表 ArrayList<Appointment> list = new ArrayList<>(Arrays.asList( new Appointment(1, 101, new Date(), "9 AM - 10 AM"), new Appointment(2, 200, new Date(), "9 AM - 10 Am"), new Appointment(3, 200, new Date(), "11 AM - 1 PM") )); // 指定要检查的条件 long targetDoctorLicenseNo = 200; Date targetDate = new Date(); String targetTime = "9 AM - 10 AM"; boolean hasAppointment = false; // 遍历列表逐一检查 for (Appointment appointment : list) { // 执照号不匹配,跳过当前对象 if (appointment.getDoctorLicenseNo() != targetDoctorLicenseNo) { continue; } // 比对日期(仅校验年月日) Calendar cal1 = Calendar.getInstance(); cal1.setTime(appointment.getDate()); Calendar cal2 = Calendar.getInstance(); cal2.setTime(targetDate); boolean isSameDate = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH); // 日期不匹配,跳过当前对象 if (!isSameDate) { continue; } // 时段匹配,标记结果并终止循环 if (appointment.getTime().equalsIgnoreCase(targetTime)) { hasAppointment = true; break; } } // 根据结果输出 if (hasAppointment) { System.out.println("该医生在指定日期和时段已有预约"); } else { System.out.println("该医生在指定日期和时段暂无预约"); } } } // Appointment类定义(需包含getter方法) class Appointment { private long id; private long doctorLicenseNo; private Date date; private String time; public Appointment(long id, long doctorLicenseNo, Date date, String time) { this.id = id; this.doctorLicenseNo = doctorLicenseNo; this.date = date; this.time = time; } // getter方法 public long getDoctorLicenseNo() { return doctorLicenseNo; } public Date getDate() { return date; } public String getTime() { return time; } }
方式2:Java 8+ Stream简化判断
如果你使用Java 8及以上版本,可以用Stream API更简洁地实现,核心判断逻辑和上面一致:
// 基于Stream的判断 boolean hasAppointment = list.stream() .anyMatch(appointment -> { if (appointment.getDoctorLicenseNo() != targetDoctorLicenseNo) { return false; } Calendar cal1 = Calendar.getInstance(); cal1.setTime(appointment.getDate()); Calendar cal2 = Calendar.getInstance(); cal2.setTime(targetDate); boolean isSameDate = cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH); if (!isSameDate) { return false; } return appointment.getTime().equalsIgnoreCase(targetTime); }); // 结果输出 if (hasAppointment) { System.out.println("该医生在指定日期和时段已有预约"); } else { System.out.println("该医生在指定日期和时段暂无预约"); }
关键逻辑说明
- 医生执照号比对:直接用
==比对long类型的属性值。 - 日期比对:通过
Calendar类提取年月日进行比对,避免Date精确到毫秒的问题。如果你的项目使用Java 8+的LocalDate替代Date,可以直接用equals()比对,会更简洁。 - 时段比对:根据需求选择
equals()(严格匹配)或equalsIgnoreCase()(忽略大小写匹配)。
内容的提问来源于stack exchange,提问作者Jesse
相关产品推荐
相关产品推荐

