转换列表<a> to Map<string,list<a>&gt; using java 8 stream API

时间:2017-09-06 15:19:07

标签: java java-8 java-stream

I have a following code:

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) 
    {
        Test t=new Test();
        t.test();
    }

    class A
    {
        int id;
        B b;

        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public B getB() {
            return b;
        }
        public void setB(B b) {
            this.b = b;
        }

    }

    class B
    {
        int id;
        String unique;

        public String getUnique() {
            return unique;
        }
        public void setUnique(String unique) {
            this.unique = unique;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
    }

    public void test()
    {
        B b1=new B();
        b1.setId(9999);
        b1.setUnique("123456789");

        B b2=new B();
        b2.setId(7777);
        b1.setUnique("123456789");

        B b3=new B();
        b3.setId(8888);
        b3.setUnique("987654321");


        A a1=new A();
        a1.setId(1);;
        a1.setB(b1);

        A a2=new A();
        a2.setId(2);;
        a2.setB(b1);


        A a3=new A();
        a3.setId(3);;
        a3.setB(b2);

        A a4=new A();
        a4.setId(4);;
        a4.setB(b2);

        A a5=new A();
        a5.setId(5);;
        a5.setB(b3);

        A a6=new A();
        a6.setId(6);;
        a6.setB(b3);

        List<A> list=new ArrayList<A>();
        list.add(a1);
        list.add(a2);
        list.add(a3);
        list.add(a4);
        list.add(a5);
        list.add(a6);



        Map<B,List<A>> map=list.stream()
                               .collect(Collectors.groupingBy(A::getB));

        //required output
        //Map<String, List<A>>

    }
}

In this example I have a class A which is having a reference of class B. The class B has a field named unique. As a source I have got the List (a1 to a6) which are referencing Object of Class B (b1, b2 and b3). The Object b1 and b2 contains same unique values. I want the output using Java 8 Stream API that will return me the Map> such that if unique value of object B which is referenced by A is same then the resulting list should contains the merge of objects of Class A in a List. e.g following should be the output

Map<String,List<A>> output = [
                                 "123456789" : [a1,a2,a3,a4],
                                 "987654321" : [a5,a6]
                             ]

1 个答案:

答案 0 :(得分:3)

修好了这一行(它是SufraceHolder,但原来是b1):

b2

正确的映射代码是这样的:

b2.setUnique("123456789");

输出(用Map<String,List<A>> map=list.stream() .collect(Collectors.groupingBy(p -> p.getB().unique)); 表示的A)是:

{ID}