I want to write below scenario using Java8. I am not sure how to explain it further, please comment if you have any doubts.
class A{ List<B> values; } class B{ } //Assume some values List<String> list = ["val1","val2"]; //Assume some values Map<String, A> map = { "val1" : [A object] , "val2" : [A object] }; B b = new B(); //Iterate over every value in list for(String each: list){ //if map has a key with the value inside the list A a = map.get(each); //A has a list called "values" of B class objects. If that list is not null or empty add a new B object (b )in our case to the a List else initialze a new List and add the value there. if(a.values != null && !a.values.isEmpty()){ a.values.add(b); }else{ List<B> bList = new ArrayList<>(); bList.add(b); a.values = bList; } }