我有一个字符串数组的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进行排序并使用相同的键(排序方式:名称,sname,Id1,Id2,类型)来获得一个新的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
}
}
谢谢,
答案 0 :(得分:1)
使用番石榴ArrayListMultiMap可能更容易做到以上。
E.g。
之类的东西names
重要提示:确保您的MyKey类实现// for each row...
myArrayListMultiMap.put(new MyKey(name, sname, id1, id2, type), LDP + "," + oldValue + "," + newValue);
// then
for (MyKey key : myArrayListMultiMap.keySet()) {
List<String> values = myArrayListMultiMap.get(key);
String concatenated = StringUtils.join(values, ";");
myList.add(new Row(entry.getKey(), concatenated));
}
Collections.sort(myList, myComparator);
和hashCode
。
答案 1 :(得分:1)
这是使用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]