我有一个字符串数组的 Arraylist,已由下面的值(列和行)填充
{"name","sname","Id1","Id2","type","LDP","oldvalue","newvalue"}
{"test1","abc","20","50","t1","SD1","0","1"}
{"test2","znc","21","23","t1","SF5","3","4"}
{"test1","abc","20","50","t1","SD3","0","1"}
{"test1","HJc","53","50","t1","SD3","0","1"}
{"test2","znc","21","23","t1","SF1","1","6"}
{"test1","abc","20","50","t1","SD5","2","19"}
{"test3","ldb","19","54","t1","SR51","6","1"}
{"test2","znc","21","23","t1","SF12","17","36"}
{"test3","ldb","19","54","t1","SR44","19","31"}
{"test4","lYI","76","56","t1","TB77","54","87"}
我想通过对当前的Arraylist进行排序并获取具有相同键的行(排序方式:name,sname,Id1,Id2,type)来获得一个新的Arraylist,将它们的值连接在一列中(用;分隔)线。
预期输出:
{"name","sname","Id1","Id2","type","Comment"}
{"test1","abc","20","50","t1","SD1,0,1; SD3,0,1; SD5,2,19"}
{"test1","HJc","53","50","t1","SD3,0,1"}
{"test2","znc","21","23","t1","SF5,3,4; SF1,1,6; SF12,17,36"}
{"test3","ldb","19","54","t1","SR44,19,31;SR51,6,1 }
{"test4","lYI","76","56","t1","TB77,54,87"}
我的Arraylist是根据结果查询生成的:
// header
String[] myString0 = {"name","sname","Id1","Id2","type","LDP","oldvalue","newvalue"};
//lines
while (rset.next()) {
String name = rset.getString("name");
String sname = rset.getString("sname");
String Id1 = rset.getString("Id1");
String Id2 = rset.getString("Id2");
String type = rset.getString("type");
String LDP = rset.getString("LDP");
String oldvalue = rset.getString("oldvalue");
String newvalue = rset.getString("newvalue");
String[] myString1 = {name, sname, Id1, Id2, "type", LDP, oldvalue, newvalue};
outerArr.add(myString1);// my Arraylist
}
}
谢谢
最佳答案
这是一个使用 Streams 的解决方案,并得到了 Guava Ordering
的一些帮助。效用:
public static List<String[]> aggregate(List<String[]> data) {
List<String[]> aggregated = data.stream()
.skip(1)
.map(Arrays::asList)
.collect(Collectors.groupingBy(
a -> a.subList(0, 5),
() -> new TreeMap<>(
Ordering.from(String.CASE_INSENSITIVE_ORDER)
.lexicographical()),
Collectors.mapping(
a -> String.join(",", a.subList(5, 8)),
Collectors.joining("; "))))
.entrySet()
.stream()
.map(e -> Stream.concat(
e.getKey().stream(),
Stream.of(e.getValue())))
.map(s -> s.toArray(String[]::new))
.collect(Collectors.toCollection(ArrayList::new));
aggregated.add(0, new String[] {"name","sname","Id1","Id2","type","Comment"});
return aggregated;
}
测试:
public static void main(String[] args) {
List<String[]> data = Arrays.asList(new String[][] {
{"name","sname","Id1","Id2","type","LDP","oldvalue","newvalue"},
{"test1","abc","20","50","t1","SD1","0","1"},
{"test2","znc","21","23","t1","SF5","3","4"},
{"test1","abc","20","50","t1","SD3","0","1"},
{"test1","HJc","53","50","t1","SD3","0","1"},
{"test2","znc","21","23","t1","SF1","1","6"},
{"test1","abc","20","50","t1","SD5","2","19"},
{"test3","ldb","19","54","t1","SR51","6","1"},
{"test2","znc","21","23","t1","SF12","17","36"},
{"test3","ldb","19","54","t1","SR44","19","31"},
{"test4","lYI","76","56","t1","TB77","54","87"}
});
aggregate(data)
.stream()
.map(Arrays::toString)
.forEach(System.out::println);
}
输出:
[name, sname, Id1, Id2, type, Comment] [test1, abc, 20, 50, t1, SD1,0,1; SD3,0,1; SD5,2,19] [test1, HJc, 53, 50, t1, SD3,0,1] [test2, znc, 21, 23, t1, SF5,3,4; SF1,1,6; SF12,17,36] [test3, ldb, 19, 54, t1, SR51,6,1; SR44,19,31] [test4, lYI, 76, 56, t1, TB77,54,87]
关于java - java数组列表中的聚合字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43770833/