使用Java 8流将子对象收集到parentObjectId的哈希图

时间:2019-11-12 01:10:40

标签: java collections java-8 java-stream

public class StreamTest {

    public static void main(String[] args){
        parentObject parentObject1 = new parentObject();
        parentObject1.setId(1);
        ArrayList<ChildObject> childObjectList = new ArrayList<ChildObject>();
        parentObject1.setChildObjectList(childObjectList);

        ChildObject childObject1 = new ChildObject();
        childObject1.setId(1);
        childObject1.setName("ce1");

        ChildObject childObject2 = new ChildObject();
        childObject2.setId(2);
        childObject2.setName("ce2");

        ChildObject childObject3 = new ChildObject();
        childObject3.setId(3);
        childObject3.setName("ce3");

        childObjectList.add(childObject1);
        childObjectList.add(childObject2);
        childObjectList.add(childObject3);

        parentObject parentObject2 = new parentObject();
        ArrayList<parentObject> parentObjectList = new ArrayList<parentObject>();
        parentObjectList.add(parentObject1);
        parentObjectList.add(parentObject2);

        parentObject2.setId(2);
        ArrayList<ChildObject> childObjectList2 = new ArrayList<ChildObject>();
        parentObject2.setChildObjectList(childObjectList2);

        ChildObject childObject4 = new ChildObject();
        childObject4.setId(4);
        childObject4.setName("ce4");

        ChildObject childObject5 = new ChildObject();
        childObject5.setId(5);
        childObject5.setName("ce5");

        ChildObject childObject6 = new ChildObject();
        childObject6.setId(6);
        childObject6.setName("ce6");

        childObjectList2.add(childObject4);
        childObjectList2.add(childObject5);
        childObjectList2.add(childObject6);


       //parentObjectID as key and childObjectList as values
        HashMap<Integer, Set<String>> parentIDchildNameMap = new HashMap<Integer, Set<String>>();
        parentObjectList.forEach(
                parentObject -> parentIDchildNameMap.put(parentObject.getId(), parentObject.getChildObjectList().stream()
                                                                                .filter(childObject -> childObject.getId()!=0)
                                                                                .map(childObject -> childObject.getName())
                                                                                .collect(toSet())));

        Set<Integer> outset = parentIDchildNameMap.keySet();

        for(Integer va : outset){
            System.out.println("key :"+ va + " value : " +parentIDchildNameMap.get(va));
        }
    }
}

我有一个parentObject,其中有一个子对象列表,我想创建一个哈希图,以父对象的id作为键,childObject的名称列表作为值。我已经编写了上面的表达式来创建它。我正在创建一个哈希图,然后自己输入值。这可行,但是有更好的方法吗?我可以使用collect直接在地图上收集它们吗?

1 个答案:

答案 0 :(得分:1)

是的,您可以使用Collectors.toMap直接收集所需的结果。可以这样实现:

Map<Integer, Set<String>> parentIdChildNameMap = parentObjectList.stream()
        .collect(Collectors.toMap(ParentObject::getId, parent -> parent.getChildObjectList().stream()
                .filter(childObject -> childObject.getId() != 0)
                .map(ChildObject::getName)
                .collect(Collectors.toSet()), (a, b) -> b));