需要对象数组列表中的映射

时间:2019-04-03 13:27:35

标签: java arrays jpa java-8 grouping

我从@NamedQuery(JPA)获取对象数组的列表

List<Object[]> restinctionList = (List<Object[]> ) 
em.createNamedQuery("DocqryRestriction.getAllAdmnAndER").getResultList();

包含两个Long值的Object数组 示例:Object[] = {12345L,34533L}

我想根据Object[0]键对列表(restinctionList)进行分组 像

Map<Long, List<Long>> groupList = restinctionList.stream().collect(Collectors.groupingBy());

我怎么写"restinctionList.stream().collect(Collectors.groupingBy(restinctionList.toArray()));"

这样我就可以得出正确的结果

已分配给对象数组的原始数据为

1 - 10068   
2 - 10069   
3 - 10070   
4 - 10080   
5 - 10100   102912

6 - 10100   102273

7 - 10100   
8 - 10124   

我希望结果为

Key 10068 value null
Key 10069 value null
...
...
key - 10100 value [null, 102912,102273]
....

4 个答案:

答案 0 :(得分:1)

为什么不使用forEach

List<List<Long>>   restinctionList =  Arrays.asList(Arrays.asList(10068L),
            Arrays.asList(10069L),
            Arrays.asList(10070L),
            Arrays.asList(10080L),
            Arrays.asList(10100L,102912L),
            Arrays.asList(10100L,102273L),
            Arrays.asList(10100L),
            Arrays.asList(10124L));

    Map< Long,List<Long>> countingrestinctionList =  new HashMap<>();
    restinctionList.forEach(list->{ 
        List<Long> tmpArrayList =   new ArrayList<>(list);
        if(countingrestinctionList.get(list.get(0))!=null) {
            List<Long> resultList=countingrestinctionList.get(list.get(0));
            tmpArrayList.remove(0);
            resultList.addAll(tmpArrayList);
        }else {
            tmpArrayList.set(0, null);
            countingrestinctionList.put(list.get(0),tmpArrayList ); 
        }
    });
    System.out.println(countingrestinctionList); 

答案:

{10080=[null], 10068=[null], 10100=[null, 102912, 102273], 10069=[null], 10070=[null], 10124=[null]}

答案 1 :(得分:1)

这里是使用单个流语句的版本

    public static void main(String[] args) {
        List<Object[]> restinctionList = Arrays.asList(
                new Object[]{10068L},
                new Object[]{10069L},
                new Object[]{10070L},
                new Object[]{10080L},
                new Object[]{10100L, 102912L},
                new Object[]{10100L, 102273L},
                new Object[]{10100L},
                new Object[]{10124L}
                );
        Map<Object, ArrayList<Object>> result = restinctionList
                .stream()
                .collect(Collectors.groupingBy(
                        (Object[] e) -> e[0],
                        Collector.of(
                                () -> {ArrayList<Object> e = new ArrayList<>(); e.add(null); return e;},
                                (ArrayList<Object> l, Object[] e) -> {if(e.length > 1){l.add(e[1]);}},
                                (ArrayList<Object> a, ArrayList<Object> b) -> {a.addAll(b); return a;})
                ))
                ;

        result.entrySet().stream().forEach(System.out::println);
    }

将哪个作为输出

10080=[null]
10100=[null, 102912, 102273]
10068=[null]
10069=[null]
10070=[null]
10124=[null]

答案 2 :(得分:0)

我找到了我的问题的答案

package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestListObjectArray {

public static void main(String[] args) {
    Object[] obj = { 42628, 567898 };
    Object[] obj1 = { 426238, 5674898 };
    Object[] obj2 = { 42621, 5678498 };
    Object[] obj3 = { 42627, 5678698 };
    Object[] obj4 = { 42627, };
    Object[] obj5 = { 42627, 1000 };

    List<Object[]> listObjectArr = new ArrayList<>();
    listObjectArr.add(obj);
    listObjectArr.add(obj1);
    listObjectArr.add(obj2);
    listObjectArr.add(obj3);
    listObjectArr.add(obj4);
    listObjectArr.add(obj5);

    Map<Object, List<Object>> ObjectMap = new HashMap<>();
    List<Object[]> restinctionList = new ArrayList<>();

    List<List<?>> restinctionDummyList = new ArrayList<>();
    for (Object[] resArr : listObjectArr) {
        List<Object> targetList = Arrays.asList(resArr);
        restinctionDummyList.add(targetList);
    }
    restinctionDummyList.forEach(list -> {
        List<Object> tmpArrayList = new ArrayList<>(list);
        if (ObjectMap.get(list.get(0)) != null) {
            List<Object> resultList = ObjectMap.get(list.get(0));
            tmpArrayList.remove(0);
            resultList.addAll(tmpArrayList);
        } else {
            ObjectMap.put(list.get(0), tmpArrayList);           
        }
    });

    System.out.println(ObjectMap);
}
}

控制台上的输出

{42627=[42627, 5678698, 1000], 42628=[42628, 567898], 426238=[426238, 5674898], 42621=[2621, 5678498]}

答案 3 :(得分:0)

好吧,如果您想使用groupingBy,可以这样做:

private Map<Long, List<Long>> getMapGroupingBy(List<Object[]> list) {
    Map<Long, List<Long>> map2 = new HashMap<>();
    map2 = list.stream().collect(Collectors.groupingBy(obs -> (Long)obs[0], Collectors.mapping(obs -> (obs.length>1?(Long)obs[1]:null), Collectors.toList())));
    return map2;
}

如果您更想获得结果,而不是使用groupingBy(),则可以这样做:

private Map<Long, List<Long>> getMapForEach(List<Object[]> list) {
    Map<Long, List<Long>> map = new HashMap<>();
    list.stream().forEach(obs -> {
        map.computeIfAbsent((Long)obs[0], a -> new ArrayList<Long>()).add(obs.length>1?(Long)obs[1]:null);
    });
    return map;
}

如果您打印map,则会得到以下内容:

{1234=[123, null, 678], 1223=[null], 1225=[345]}

您的输入列表应如下所示:

List<Object[]> list = new ArrayList<>();
list.add(new Object[] {1234l,123l});
list.add(new Object[] {1234l});
list.add(new Object[] {1234l,678l});
list.add(new Object[] {1223l});
list.add(new Object[] {1225l,345l});