Prerak Tiwari
Prerak Tiwari

Reputation: 3466

converting List<A> to Map<String,List<A>> using java 8 stream API

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

Upvotes: 3

Views: 159

Answers (1)

jrtapsell
jrtapsell

Reputation: 7001

Having fixed this line (it was b1, but was meant to be b2):

b2.setUnique("123456789");

the correct mapping code is this:

Map<String,List<A>> map=list.stream()
    .collect(Collectors.groupingBy(p -> p.getB().unique));

The output (with A's represented by {ID}) is this:

{123456789=[{1}, {2}, {3}, {4}], 987654321=[{5}, {6}]}

Upvotes: 3

Related Questions