我有如下数据的对象列表:
我需要基于满足以下条件的唯一 NurseId 记录的输出:
所以上面列表的最终输出是这样的: when updatedDt3 > updatedDt1
感谢解决这个问题。
尝试使用 Java 流和嵌套循环但没有用。
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);
}