In Java, how to get list down the latest/first item of each group after grouping by field(/s)? e.g From the given list of medical records, how to retrieve the latest record of each patient.
Sample Data
---------------------------------------------------------------------------
PatientName | ReportDate | ReadingNo | Measurement
---------------------------------------------------------------------------
X | 2020-01-02 | 1 | 255
Y | 2020-01-02 | 1 | 250
X | 2020-01-02 | 2 | 266
Y | 2020-01-02 | 2 | 270
X | 2020-01-02 | 3 | 298
Y | 2020-01-02 | 3 | 259
X | 2020-01-02 | 4 | 280
Y | 2020-01-02 | 4 | 285
X | 2020-01-03 | 1 | 260
Y | 2020-01-03 | 1 | 265
X | 2020-01-03 | 2 | 280
Y | 2020-01-03 | 2 | 260
X | 2020-01-03 | 3 | 285
Y | 2020-01-03 | 3 | 290
X | 2020-01-03 | 4 | 290
Y | 2020-01-03 | 4 | 280
---------------------------------------------------------------------------
Expected result
---------------------------------------------------------------------------
PatientName | ReportDate | ReadingNo | Measurement
---------------------------------------------------------------------------
X | 2020-01-03 | 4 | 290
Y | 2020-01-03 | 4 | 280
---------------------------------------------------------------------------
I can do it C# as below. Any one can help to translate it in Java?
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
Console.WriteLine("---------------------------------------------------------------------------");
Console.WriteLine(String.Format("{0} |\t{1}\t| {2} |\t{3}", "PatientName", "ReportDate", "ReadingNo", "Measurement"));
Console.WriteLine("---------------------------------------------------------------------------");
List lst = new List
{
new Patient(){Name="X", ReportDate="2020-01-02", ReadingNo=1, Measurement=255 },
new Patient(){Name="Y", ReportDate="2020-01-02", ReadingNo=1, Measurement=250 },
new Patient(){Name="X", ReportDate="2020-01-02", ReadingNo=2, Measurement=266 },
new Patient(){Name="Y", ReportDate="2020-01-02", ReadingNo=2, Measurement=270 },
new Patient(){Name="X", ReportDate="2020-01-02", ReadingNo=3, Measurement=298 },
new Patient(){Name="Y", ReportDate="2020-01-02", ReadingNo=3, Measurement=259 },
new Patient(){Name="X", ReportDate="2020-01-02", ReadingNo=4, Measurement=280 },
new Patient(){Name="Y", ReportDate="2020-01-02", ReadingNo=4, Measurement=285 },
new Patient(){Name="X", ReportDate="2020-01-03", ReadingNo=1, Measurement=260 },
new Patient(){Name="Y", ReportDate="2020-01-03", ReadingNo=1, Measurement=265 },
new Patient(){Name="X", ReportDate="2020-01-03", ReadingNo=2, Measurement=280 },
new Patient(){Name="Y", ReportDate="2020-01-03", ReadingNo=2, Measurement=260 },
new Patient(){Name="X", ReportDate="2020-01-03", ReadingNo=3, Measurement=285 },
new Patient(){Name="Y", ReportDate="2020-01-03", ReadingNo=3, Measurement=290 },
new Patient(){Name="X", ReportDate="2020-01-03", ReadingNo=4, Measurement=290 },
new Patient(){Name="Y", ReportDate="2020-01-03", ReadingNo=4, Measurement=280 }
};
lst.ForEach(p=>{
Console.WriteLine(p.toString());
});
Console.WriteLine("---------------------------------------------------------------------------");
var lstLatest = from p in lst
group p by p.Name
into g
select g.OrderByDescending(p => p.ReportDate).ThenByDescending(p=>p.ReadingNo).ToList();
foreach(var p in lstLatest)
{
Console.WriteLine(p.First().toString());
}
}
}
public class Patient
{
public string Name { get; set;}
public string ReportDate {get;set;}
public int ReadingNo {get;set;}
public int Measurement {get;set;}
public string toString()
{
return String.Format("{0}\t\t\t|\t{1}\t|\t{2} \t|\t{3}", this.Name, this.ReportDate, this.ReadingNo, this.Measurement);
}
}