基于两个日期条件过滤嵌套列表,一个在 Java 中由另一个回退

问题描述 投票:0回答:1

我有如下数据的对象列表:

  1. Patient1-Nurse1-createdDt1-updatedDt1
  2. Patient1-Nurse2-createdDt2-updatedDt2
  3. Patient2-Nurse1-createdDt1-updatedDt3
  4. Patient2-Nurse3-createdDt1-updatedDt4
  5. Patient3-Nurse4-createdDt4-updatedDt5

我需要基于满足以下条件的唯一 NurseId 记录的输出:

  • 如果有记录意味着一个 NurseId 映射到两个患者,请考虑最新的 updatedDt 并删除另一条记录
  • 如果比较记录中没有updatedDt,考虑第二个
  • 如果 updatedDt 不存在于两个记录中,则考虑最新创建的 Dt 记录

所以上面列表的最终输出是这样的: when updatedDt3 > updatedDt1

  1. Patient1-Nurse2-createdDt2-updatedDt2
  2. Patient2-Nurse1-createdDt1-updatedDt3
  3. Patient2-Nurse3-createdDt1-updatedDt4
  4. Patient3-Nurse4-createdDt4-updatedDt5

感谢解决这个问题。

尝试使用 Java 流和嵌套循环但没有用。

java filter stream compare reduce
1个回答
0
投票
Got the solution to it, here is my usecase:
I have the patientList of Patient objects like this:
    patientList = {p1,p2,p3,p4}
    each patient object consist of properties like patientId, nurseId, createDt, updateDt.
    Requirement: In this one nurse can be associated to multiple patients and need only one record where nurse is associated with patient which has a latest update or create.
    Condition: first need to consider the latest update date if its not present consider the latest create date.
    
First step: 
groupby the patientList based on nurseId
 
    Map<String, List<CGR>> patientList = 
        list.stream().collect(Collectors.groupingBy(n-> n.getNurse().getNurseId()))

Second step: 
List<Patient> resultList = new ArrayList<>();
for(List<Patient> innerList: patientList.values()) {
   Patient latestPatient = Collections.max(innerList, Comparator.comparing(c ->
                    (c.getUpdateDt()!=null) ? (c.getUpdateDt())
                            : c.getCreateDate()));
            resultList.add(latestPatient);
        }
© www.soinside.com 2019 - 2024. All rights reserved.