2

I find this use case difficult to construct the expected map structure. Here is my use case - Let's say I have 8 quote line items with Group custom picklist field as below.

Group Name    Quantity
Group1          100
Group2           30
Group2           30
Group2           30
Group2          100
Group2           2
Group2           2
Group2          100

Now, I would like to construct a Map with String, Integer pair like below

KEY         VALUE
Group1-100, 1
Group2-30, 3
Group2-100, 1
Group2-2, 2
Group2-100, 1

The same consecutive quantities should be counted.

I have tried:

Decimal previousQuantity=0;
integer i=1;
for(QuoteLine__c lineItem : quoteLines){
    if(previousQuantity == lineItem.Quantity__c){
       i++;
    }else{
       previousQuantity = lineItem.Quantity__c;
    }
 }
 quantityHelperMap.put(lineItem.groupName__c+'-'+string.valueOf(previousQuantity), i);

Any help would be appreciated!. Thanks.

3
  • 1
    Why is Group2-100, 1 repeated? If its a type then it can be easily solved with Map<String, Integer> where key is GroupName+Quantity and its value is count of the matching key.
    – Raul
    Commented May 18, 2020 at 12:18
  • 3
    What have you tried so far? Commented May 18, 2020 at 12:30
  • Updated with what I have tried so far.
    – Prince
    Commented May 18, 2020 at 12:40

3 Answers 3

1

You might find it easier to use a non-String key instead:

// Use Group-Quantity as a composite key //
Map<QuoteLine__c, Integer> groupQtyMap = new Map<QuoteLine__c, Integer>();
for(QuoteLine__c record: quoteLines) {
  QuoteLine__c key = new QuoteLine__c(Group__c=record.Group__c, Quantity__c=record.Quantity__c);
  Integer counter = groupQtyMap.get(key);
  // Key was not in the map yet //
  if(counter == null) {
    counter = 0;
  }
  groupQtyMap.put(key, counter+1);
} 
1

I think you're inadvertently making this harder than it needs to be.

In the end, it boils down to a few simple steps:

  • Generate the key for the current iteration
  • Check to see if your map contains that key (add it if it doesn't)
  • Update the value
Map<String, Integer> groupQtyToCountMap = new Map<String, Integer>();

for(QuoteLine__c lineItem : quoteLines){
    // Generate the key
    String key = lineItem.Group_Name__c + '-' + lineItem.Quantity.toString();

    // Check if it exists in your map
    if(!groupQtyToCountMap.containsKey(key)){
        // add it to the map if it doesn't exist
        groupQtyToCountMap.put(key, 0);
    }

    // Here, we're guaranteed to have the key in the map, so we don't
    //   need to do any more checking.
    groupQtyToCountMap.put(key, groupQtyToCountMap.get(key)++);
}

There's a bit more work involved (we need to call both get() and put()) to update the value of a map when you're using a primitive data type (like String, Integer, etc...) because those are generally stored by value instead of by reference.

An alternative approach here would be to center the logic around the (possibly null) Integer value. That ends up saving a little bit of typing.

Map<String, Integer> groupQtyToCountMap = new Map<String, Integer>();

for(QuoteLine__c lineItem : quoteLines){
    // Generate the key
    String key = lineItem.Group_Name__c + '-' + lineItem.Quantity.toString();

    // Check if it exists in your map
    Integer value = groupQtyToCountMap.get(key);
    if(value == null){
        // The important bit here is that we set the value so we can operate on it later
        value = 0;
    }

    // Since we're going to put() in each iteration anyway, just do it once at the end
    groupQtyToCountMap.put(key, value++);
}
0

You cannot create a map with the key you specify in your example because they are duplicated. (line 3 and line 5 Group2-100)

KEY         VALUE
Group1-100, 1
Group2-30,  3
Group2-100, 1
Group2-2,   2
Group2-100, 1    
2
  • No problem, if duplicated/overridden.
    – Prince
    Commented May 18, 2020 at 13:52
  • But would you like to restart or continue the counter once you find the same key again? (non-consecutive) Commented May 18, 2020 at 13:57

You must log in to answer this question.

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