2

I want to get a List<Integer> out of Map<Integer, Set<Integer>>.

Map<Integer, Set<Integer>> MAPAgreementAdhoc = new Map<Integer, Set<Integer>>{1 => new set<Integer>{1,2,3}, 2 => new Set<Integer>{2,3,4}};

My code to get a list of Values from map.values is as below but it's not complete. I need help to move forward.

Motive of this code is to avoid nested for loop.

Map<Integer, Set<Integer>> mapEnhancement = new Map<Integer, Set<Integer>>();
Map<Integer, Set<Integer>> MAPAgreementAdhoc = new Map<Integer, Set<Integer>>{1 => new set<Integer>{1,2,3}, 2 => new Set<Integer>{2,3,4}};
List<Integer> AgreementAttachment = new List<Integer>{1,2,3,4,5,6};
for(Integer oTemp : AgreementAttachment){
    if(MAPAgreementAdhoc.containsKey(oTemp)){

        Set<Integer> setEnhance = new Set<Integer>();

        setEnhance = MAPAgreementAdhoc.get(oTemp);

        mapEnhancement.put(oTemp, setEnhance);

    }
}
//for(Integer tempId : mapEnhancement.values()){
    system.debug(mapEnhancement.values());
//}

Output: DEBUG|({1, 2, 3}, {2, 3, 4})

Expected Output: {1,2,2,3,3,4}

1 Answer 1

2

I'm typing this straight in the browser, but couldn't you just do:

List<Integer> results = new List<Integer>();
List<Integer> AgreementAttachment = new List<Integer>{1,2,3,4,5,6};

for(Integer index : AgreementAttachment)
{
  if(MAPAgreementAdhoc.get(index) != null)
  {
    results.addAll(MAPAgreementAdhoc.get(index));
  }
}

System.debug(results);

Of course, if you actually need the other Map of Sets you'll need to create & populate that too, but if it's just the list then this code will do what you need.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .