1

I have an apex class that is parsing json responses from a few apis.

The result is having a set and a list from the responses and I would like to map them together where the map key would be the value in the set and I would add each instance of list wrapper where one of the list value matches the set value. I have included my set, list, and wrapper below.

I parsed these two results with my own wrapper, seen below by creating a set and a list.

public class udcName{
        @AuraEnabled public string orderudcname {get;set;}
    }
    
    public class udc {
        @AuraEnabled public string  udcname      {get;set;}
        @AuraEnabled public Integer orderrevnum  {get;set;}
        @AuraEnabled public string  orderudcval  {get;set;}
        @AuraEnabled public string  orderoptdesc {get;set;}
    }

Set

{udcName:[orderudcname=ABC], udcName:[orderudcname=DEF], udcName:[orderudcname=GHG], udcName:[orderudcname=LRR], udcName:[orderudcname=GVW]}

List

(udc:[orderoptdesc=ABC Code, orderrevnum=0, orderudcval=1, udcname=ABC], udc:[orderoptdesc=ABC Code, orderrevnum=1, orderudcval=1, udcname=ABC], udc:[orderoptdesc=None, orderrevnum=0, orderudcval=1, udcname=DEF], udc:[orderoptdesc=None, orderrevnum=0, orderudcval=1, udcname=GHG], udc:[orderoptdesc=No, orderrevnum=0, orderudcval=1, udcname=LRR], udc:[orderoptdesc=No Selection, orderrevnum=0, orderudcval=19, udcname=GVW], udc:[orderoptdesc=10000, orderrevnum=1, orderudcval=19, udcname=GVW]

There can be 0-4 list values appear for a single value in the set. Anytime orderudcname in the set is equal to udcname in the list I would like to map the wrapper instance to a map with the key being the value of orderudcname in the set.

Attempted map mock result: {udcName: ABC, {udc:[orderoptdesc=ABC Code, orderrevnum=0, orderudcval=1, udcname=ABC], udc:[orderoptdesc=ABC Code, orderrevnum=1, orderudcval=1, udcname=ABC]}

1 Answer 1

1

As a most literal rendering, you can:

Map<udcName, List<udc>> udcByName = new Map<udcName, List<udc>>();
for(udcName item: udcSet) {
  udcByName.put(item, new List<udc>());
}
for(udc item: udcList) {
  udcByName.get(new udcName(item.udcname)).add(item);
}

To make this work, you just need to add a bit of logic to your wrappers:

public class udcName implements Comparable {
  @AuraEnabled public string orderudcname {get;set;}
  public udcName() {} // zero-arg constructor if you need it
  public udcName(String udcName) {
    orderudcname = udcName;
  }
  // Next two methods allow udcName to be used as a key in a Map
  public Boolean equals(Object other) {
    return orderudcname == ((udcName)other).orderudcname;
  }
  public Integer hashCode() {
    return orderudcname.hashCode();
  }
  public Integer compareTo(Object other) {
    udcName otherName = (udcName)other;
    if(orderudcname == otherName.orderudcname) {
      return 0;
    }
    if(orderudcname < otherName.orderudcname) {
      return -1;
    }
    return 1;
  }
}

Of course, you could have also just used Map<string, List<udc>> instead, but having the ability to use the set items may be useful.

3
  • Thanks @sfdcfox. The map looks like exactly what I wanted, but iterating over it in VFP is producing a 'This map cannot be used in an iteration because the keys cannot be sorted'. The keys are unique. I would think they could be sorted? Any pointers for handling this one?
    – Wes
    Aug 23, 2022 at 15:41
  • @Wes That error means you need Comparable. See my edit.
    – sfdcfox
    Aug 23, 2022 at 17:04
  • Thanks @sfdcfox. It worked like a charm.
    – Wes
    Aug 23, 2022 at 18:19

You must log in to answer this question.

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