我有一个从迭代创建的arraylist
ArrayList<String> ulpList = new ArrayList<String>();
String record = id + "," + lp.getTime() + "," + lp.getLatitude() + "," + lp.getLongtitude() + "," + lp.getPoint() + ", " + lp.getDistance();
ulpList.add(record);
同一ID的实例很多,但时间,纬度,经度,点和距离不同。因此,我想将arraylist ulplist分为多个子列表。然后,我想按时间对这些子列表进行排序/排序。在按时间对子列表进行排序之后,我要执行一些计算,然后将它们重新结合起来,并将结果输出到.csv文件中。
我只需要知道如何将arraylist分解成我想要的这些子列表(这将是临时的),然后如何根据时间(在第一个
,
分隔符之后)对它们进行排序。样本输出:
[[04ae46c177169feac5f697eexxxx,1418601075,1.375579,103.960797,null, 1000.0]]
[[04ae46c177169feac5f697eexxxx,1418602016,1.381164,103.966164,null, 1000.0]]
[[04ae46c177169feac5f697eexxxx,1418603148,1.381164,103.966164,null, 1000.0]]
[[04ae46c177169feac5f697eexxxx,1418601994,1.381164,103.966164,null, 1000.0]]
[[055ee328d4d297500e9a7f4cffe6xxxx,1418602721,1.313564,103.878443,null, 1000.0]]
[[055ee328d4d297500e9a7f4cffe6xxxx,1418602119,1.313564,103.878443,null, 1000.0]]
[[055ee328d4d297500e9a7f4cffe6xxxx,1418601901,1.313564,103.878443,null, 1000.0]]
[[055ee328d4d297500e9a7f4cffexxxx,1418600991,1.313564,103.878443,null, 1000.0]]
[[055ee328d4d297500e9a7f4cffe6xxxx,1418600132,1.313564,103.878443,null, 1000.0]]
[[00cd34bad39d19f8e2a335b444bxxxx,1418600273,1.345569,103.696973,null, 1000.0]]
[[04036dd2f45253bc9c24810f8e3exxxx,1418603285,1.301047,103.853357,null, 1000.0]]
最佳答案
您可以使用Java 8流,例如:
Map<String, List<String>> groups = ulpList.stream().collect(Collectors.groupingBy(e -> e.split(",")[0]));
ArrayList<List<String>> sorted = new ArrayList<>();
for (List<String> s : groups.values()) {
sorted.add(s.stream().sorted((s1, s2) -> {
long ts1 = Long.valueOf(s1.split(",")[1]);
long ts2 = Long.valueOf(s2.split(",")[1]);
return Long.compare(ts1, ts2);
}).collect(Collectors.toList()));
}
// do calculations
// ...
当然,您可以将string更改为string []以减少拆分次数,并以更有效和优雅的方式编写它,但是我想保持简单