对 CSV 分组后将成员合并成字符串
【问题】
I have a csv file that is ‘|’ delimited and has multiple lines in it. Some entries in the file have duplicates and I need to merge these entries into a single line. These entries will then be used to create a map object. I am new to java and need help on how to do this.. I created a map object but don’t know how to handle the duplicates. Can anyone suggest how to do this in java?
My list:
HR |325|50051710|CN=ADGroup1
HR |325|50051710|CN=ADGroup2
BA |375|50110084|CN=ADGroup1
SYS ADMIN |877|50145471|CN=ADGroup2
Output has to be like this to be read as map object:
HR |325|50051710|CN=ADGroup1,CN=ADGroup2
BA |375|50110084|CN=ADGroup1
...
My code:
Map attrList = new HashMap();
String[] record = line.split("\\|");
try{
br1 = new BufferedReader(new FileReader(inputFile));
String line = "";
while ((line = br1.readLine()) != null) {
if (record[1] != null ) {
Map map = new HashMap();
String costcentre = record[1].trim();
map.put("Costcentre", costcentre);
String jobcode = record[2].trim();
map.put("Jobcode", jobcode);
String adgroup = record[3].trim();
map.put("ADGroup", adgroup);
attrList.put(costcentre + jobcode, map);
【回答】
这里需要进行简单的结构化计算:按照第 1、2、3 列分组,再将组内的第 4 列用逗号拼在一起。但 JAVA 缺乏相关的类库,实现过程复杂,代码可读性差。这种情况可以用集算器辅助实现,代码更直观易懂:
A |
|
1 |
=file("test.csv").import(;,"|") |
2 |
=A1.group(_1,_2,_3;~.(_4).concat@c()) |
A1: 用分隔符 "|" 读取文件。
A2: 按前 3 列分组,再将每组 (即 ~) 的第 4 列用逗号拼在一起。
集算器提供 JDBC 接口,可以像数据库一样使用,Java 如何调用 SPL 脚本。