对象的分组列表,其中每个对象都是 2 个元素数组
Grouping list of objects where each object is 2 elements array
List<Object[]> values = query.getResultList();
//values = [obj1,obj2,obj3]
//obj1 = "1", "01";
//obj2 = "1", "02";
//obj3 = "1:, "03";
Map<String, List<String>> resultMap = values.stream().collect(Collectors.groupingBy(???));
我需要一个分组为 - {"1",["01","02","03"]}
的地图
我看到的参考资料很少,但似乎没有任何效果。我应该用什么来代替“???” ?如果您需要其他任何东西,请发表评论。
提前致谢。
参考:
Shortcut for adding to List in a HashMap
如果你的对象[]是字符串[]。我想代码是这样的:
package com.test.dal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class LTest {
public static void main(String[] args) {
List<String[]> values = getResultList();
//values = [obj1,obj2,obj3]
//obj1 = "1", "01";
//obj2 = "1", "02";
//obj3 = "1:, "03";
Map<String, List<String>> resultMap = values.stream().collect(Collectors.groupingBy(o -> ((String[]) o)[0], Collectors.mapping(o -> ((String[]) o)[1], Collectors.toList())));
System.out.println(resultMap);
}
public static List<String[]> getResultList() {
String[][] inits = {
{
"1", "01"
}, {
"1", "02"
}, {
"1", "03"
}
};
List<String[]> tbList = Arrays.asList(inits);
return tbList;
}
}
List<Object[]> values = query.getResultList();
//values = [obj1,obj2,obj3]
//obj1 = "1", "01";
//obj2 = "1", "02";
//obj3 = "1:, "03";
Map<String, List<String>> resultMap = values.stream().collect(Collectors.groupingBy(???));
我需要一个分组为 - {"1",["01","02","03"]}
的地图我看到的参考资料很少,但似乎没有任何效果。我应该用什么来代替“???” ?如果您需要其他任何东西,请发表评论。 提前致谢。
参考:
Shortcut for adding to List in a HashMap
如果你的对象[]是字符串[]。我想代码是这样的:
package com.test.dal;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class LTest {
public static void main(String[] args) {
List<String[]> values = getResultList();
//values = [obj1,obj2,obj3]
//obj1 = "1", "01";
//obj2 = "1", "02";
//obj3 = "1:, "03";
Map<String, List<String>> resultMap = values.stream().collect(Collectors.groupingBy(o -> ((String[]) o)[0], Collectors.mapping(o -> ((String[]) o)[1], Collectors.toList())));
System.out.println(resultMap);
}
public static List<String[]> getResultList() {
String[][] inits = {
{
"1", "01"
}, {
"1", "02"
}, {
"1", "03"
}
};
List<String[]> tbList = Arrays.asList(inits);
return tbList;
}
}