分组对象列表,其中每个对象是2个元素数组 [英] Grouping list of objects where each object is 2 elements array

查看:60
本文介绍了分组对象列表,其中每个对象是2个元素数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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"]}

I need a map grouped as - {"1",["01","02","03"]}

我看到的参考资料很少,但似乎没有任何作用.我应该用什么代替"???"? ?如果您还有其他需要,请发表评论. 预先感谢.

I have seen few references but nothing seems to work. what should i put in place of "???" ? if you need anything else , kindly comment. Thanks in advance.

ref:

在HashMap中添加到列表的快捷方式

按数组列表分组

推荐答案

如果您的对象[]是字符串[].我认为代码是这样的:

If your object[] is the string[].I think the code is this:

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;
    }
}

这篇关于分组对象列表,其中每个对象是2个元素数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆