Home > Mobile >  Comparing multiple object attributes in an arraylist of objects
Comparing multiple object attributes in an arraylist of objects

Time:01-02

I need help coming up with an if-else condition statement to compare multiple object attributes in an arraylist of objects.

For example, say I have an arraylist of appointments

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")
));

This is the Appointment class:

public class Appointment {

    private long id;
    private long doctorLicenseNo;
    private Date date;
    private String time;

//constructor   getters and setters

}

I want to check if a doctor with a particular license number is booked on a specific date and time. Here the doctor license number, date and time are given.

CodePudding user response:

As said in the comments, I suppose you find it not working because the for-loop is erasing the label each time.

Also, this code is overly complicated. You can do something like:

boolean isDoctorBooked = list.stream()
    .filter(a -> a.getDoctorLicenseNo() == licenseNo)
    .anyMatch(a -> a.getDateTime() == dateTime);

Assuming you replaced the date and time with the proper object, as per @rzwitserloot comment.

  • Related