Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java流列表<;对象[]>;映射<;对象、列表<;对象[]>&燃气轮机;_Java_Stream_Java Stream - Fatal编程技术网

java流列表<;对象[]>;映射<;对象、列表<;对象[]>&燃气轮机;

java流列表<;对象[]>;映射<;对象、列表<;对象[]>&燃气轮机;,java,stream,java-stream,Java,Stream,Java Stream,我有列表内部连接MySQL查询,我需要创建一个映射键id和值对象 下面的代码可以工作,但是如何在流中做到最好 Map<Object, List<Object[]>> convert(List<Object[]> objects) { Map<Object, List<Object[]>> objectListMap = objects.stream() .collect(Collectors.toMap(obj

我有
列表
内部连接MySQL查询,我需要创建一个映射id和值对象

下面的代码可以工作,但是如何在流中做到最好

Map<Object, List<Object[]>> convert(List<Object[]> objects) {
    Map<Object, List<Object[]>> objectListMap = objects.stream()
        .collect(Collectors.toMap(obj -> obj[0], obj -> {
            List<Object[]> list = new ArrayList<>();
            list.add(obj);
            return list;
        }, (obj1, obj2) -> {
            obj1.addAll(obj2);
            return obj1;
        }));
    return objectListMap;
}


Object[] objects structure:
objects[0] = person id
...
objects[10] = event id
...
objects[15] = event name
Map转换(列出对象){
Map objectListMap=objects.stream()
.collect(收集器.toMap(obj->obj[0],obj->{
列表=新的ArrayList();
列表。添加(obj);
退货清单;
},(obj1,obj2)->{
obj1.addAll(obj2);
返回obj1;
}));
返回objectListMap;
}
对象[]对象结构:
对象[0]=个人id
...
对象[10]=事件id
...
对象[15]=事件名称
此查询找到了访问事件的所有人员,但对象数组中从0到10的索引可能是相同的,11-15总是变化的

我想合并所有具有相同id的对象列表(对象[0])

下一步为中的每个值 地图> 转换为POJO:

PersonWithEvent converToEntity(List<Object[]> objects) {
Optional< PersonWithEvent > personWithEvent =                     
objects.stream()
.findFirst().map(obj -> {
    PersonWithEvent p = new PersonWithEvent();
    p.setId((Integer) obj[0]);
    ...
    return p;
});
personWithEvent.ifPresent(p -> {
    List<PersonEvent> personEvents = objects.stream()
            .map(obj -> {
                PersonEvent pe = new PersonEvent();
                pe.setName((String) obj[2]);
                ...
                return pe;
            })
            .collect(Collectors.toList());
    p.setPersonEvents(personEvents);
 });
 return personWithEvent.get();
PersonWithEvent converToEntity(列出对象){
可选PersonWithEvent=
objects.stream()
.findFirst().map(对象->{
PersonWithEvent p=新PersonWithEvent();
p、 setId((整数)obj[0]);
...
返回p;
});
有事件的人如果在场(p->{
List personEvents=objects.stream()
.map(obj->{
PersonEvent pe=新PersonEvent();
pe.setName((字符串)obj[2]);
...
返回pe;
})
.collect(Collectors.toList());
p、 设置个人事件(个人事件);
});
return personWithEvent.get();

是否可以对一个流执行此操作?

似乎需要在数组
对象的索引0处按元素分组

collector.groupingBy
按键分组

Map<Object, List<Object[]>> map = objects.stream()
    .collect(Collectors.groupingBy(o -> o[0]));

您可以看到
0
有两个
列表
值,它们按

分组。我将使整个过程更加明确和干净。使用带有大量注释和良好变量名的混合样式

当涉及到阅读、理解和维护代码时,这应该会有很大帮助

Map<Object, List<Object[]>> convert(List<Object[]> entities) {
    Map<Object, List<Object[]>> idToEntitesWithSameId = new HashMap<>();

    entities.forEach(entity -> {
        // Create an entry for each entity,
        // merge with entities of same id
        Object id = entity[0];

        // Get current value or empty list
        List<Object[]> entitiesWithId = idToEntitesWithSameId
            .computeIfAbsent(id, ArrayList::new);

        // Add own entity, merge with other
        // entities of same id
        Arrays.stream(entity).forEach(entitiesWithId::add);
    });

    return idToEntitesWithSameId;
}
映射转换(列出实体){
Map idToEntitesWithSameId=新HashMap();
实体。forEach(实体->{
//为每个实体创建一个条目,
//与相同id的实体合并
对象id=实体[0];
//获取当前值或空列表
列出ID为idToEntitesWithSameId的实体
.ComputeFabSent(id,ArrayList::new);
//添加自己的实体,与其他实体合并
//相同id的实体
Arrays.stream(entity).forEach(entitiesWithId::add);
});
将ID返回给具有SAMEID的实体;
}

你能解释一下所需的结构吗?从你的代码中很难分辨出来。你打算如何转换
列表
?映射的键和值是什么?
合并
函数应该做什么?每个
对象[]
数组在索引
0
处有一个id,您希望将此id映射到整个数组的其余部分,就像
对象[0]=>对象一样。此外,您希望合并具有相同id的所有对象列表(
对象[0]
)。是的,我希望合并具有相同id的所有对象列表(对象[0])<代码>收集器>按/<代码>分组实际上是一个很好和干净的方法。以前从来没有用过它,但它似乎值得学习。您可以考虑使用过滤器<代码>。过滤器(对象::非空)
用于删除
null
值。以防您不知道该方法。@Zabuza是的,但我们也需要筛选空数组,对吗here@alex请投票并接受这个答案,如果它回答了你的问题
List<Object[]> objects = new ArrayList<>();
objects.add(new Object[] { 0, 1, 2, 3, 4, 5 });
objects.add(new Object[] { 1, 1, 2, 3, 4, 5 });
objects.add(new Object[] { 2, 1, 2, 3, 4, 5 });
objects.add(new Object[] { 0, 6, 7, 8, 9 });

Map<Object, List<Object[]>> map = objects.stream()
    .collect(Collectors.groupingBy(o -> o[0]));

System.out.println(map);
{
    0 = [[Ljava.lang.Object;@378bf509,
            [Ljava.lang.Object;@5fd0d5ae],
    1 = [[Ljava.lang.Object;@2d98a335],
    2 = [[Ljava.lang.Object;@16b98e56]
}
Map<Object, List<Object[]>> convert(List<Object[]> entities) {
    Map<Object, List<Object[]>> idToEntitesWithSameId = new HashMap<>();

    entities.forEach(entity -> {
        // Create an entry for each entity,
        // merge with entities of same id
        Object id = entity[0];

        // Get current value or empty list
        List<Object[]> entitiesWithId = idToEntitesWithSameId
            .computeIfAbsent(id, ArrayList::new);

        // Add own entity, merge with other
        // entities of same id
        Arrays.stream(entity).forEach(entitiesWithId::add);
    });

    return idToEntitesWithSameId;
}