流式传输java对象列表,并在条件满足时中断

时间:2017-07-16 12:40:13

标签: java for-loop

我有一个关于通过java对象列表进行流式传输的问题,如果满足条件则会中断,

我有一个像下面这样的实体城市

@Entity
public class City {

private Country country;
private ClosestCity closestCity;
private NearestCity nearestCity;
private SisterCity sisterCity;

} 

ClosestCity,NearestCity和SisterCity是实体城市中具有外键的城市。 我有一份位于不同城市的医生名单,我想将病人分配给医生,他的城市最接近病人的城市,否则分配到最近的城市,否则分配到姐妹城市,如果没有以上问题不符合,把病人列入等候名单。 下面是我分配的方法,它不起作用。

public boolean allocateDoctorToPatient(Patient patient) {

    Practitioner closestPractitioner = null;
    List<Practitioner> practitioners = practitionerService.findAll().get();
    for (Practitioner p : practitioners) {
        //same city
        if (closestPractitioner != null) {
            for (Practitioner p1 : practitioners) {

                if (patient.getCity().equals(p1.getCity())) {
                    closestPractitioner = p1;
                    break;
                }
            }
        }
        //loops to find closest city
         else if (closestPractitioner == null) {
            for (Practitioner p2 : practitioners) {

                if (patient.getCity().equals(p2.getCity().getClosestCity())) {
                    closestPractitioner = p2;
                    break;
                }
            }
        }
        //loops to find nearest city
        else if (closestPractitioner == null) {
            for (Practitioner p3 : practitioners) {

                if (patient.getCity().equals(p3.getCity().getNearestCity())) {
                    closestPractitioner = p3;
                    break;
                }
            }
        }
        //loops to find sister city
        else if(closestPractitioner == null) {
            for (Practitioner p4 : practitioners) {

                if (patient.getCity().equals(p4.getCity().getSisterCity())) {
                    closestPractitioner = p4;
                    break;
                }
            }
        }



}
   if (closestPractitioner != null) {
        Allocate allocate =new Allocate();

        allocate.setPatient(patient);
        allocate.setPractitioner(closestPractitioner)
        save(allocate);

    }
    else {
       WaitingList waitingList=new WaitingList();
       waitingList.setPatient(patient);
       waitingListService.save(waitingList);
   }

    return closestPractitioner != null;
}

1 个答案:

答案 0 :(得分:0)

尝试使用算法:

  1. 获取适合患者的城市列表。

  2. 从步骤1获取位于城市中的从业者实体列表(使用HQL或标准API)

  3. 选择最接近患者的医生。