拆分字串结构化后分组后在组内计算

【问题】

I have an arraylist created from an iteration

ArrayList<String> ulpList = new ArrayList<String>();
String record = id + "," + lp.getTime() + "," + lp.getLatitude() + "," + lp.getLongtitude() + "," + lp.getPoint() + ", " + lp.getDistance();
ulpList.add(record);

There will be many instances of the same id, with different times, latitudes, longitudes, points and distances. Thus I want to break the arraylist ulplist into various sublists. Then, I want to order/sort these sublists by time. After I have sorted the sublists by time, I want to perform some calculations and then join them back and output the result into a .csv file. I just need to know how to break up the arraylist into these sublists that I want (which will be temporary) then how to sort them according to time, which is after the first , delimiter.

Sample output:

[[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]]

【回答】

复述过程:去掉第1和第6个字段的无用字符,按第1个字段分组,对每组数据按第2列排序,再对每组数据做计算,最后合并,这一系列动用JAVA硬编码会非常复杂。可以用SPL实现:


A

1

= file("d:\\source.csv").import@c()

2

=A1.run(#1=replace(#1,"[[",""),#6=replace(#6,"]]",""))

3

=A2.group(#1).(~.sort(#2))

4

=A3.conj(~.to(2,4))

5

= file("d:\\result.csv").export(A4)

 

A1:读取csv文件的内容。

undefined

A2:去掉第1个字段和第6个字段中的括号。

undefined

A3:按照第1个字段中的数据进行分组,并对每组数据按照第2个字段排序。

A4:对每组数据做计算,最后合并。该问题没有说明具体的组内算法,所以A4只是示意性的取每组第24行记录。

A5:将A4计算结果导入到csv文件。

 

上述代码很容易和JAVA集成(可参考Java 如何调用 SPL 脚本