1

I have a List of type wrapper class having two variables let's say col1 and col2. I want to append all col2 values if their col1 is the same and store it as col 1 and col 2 in a new variable. Here is the data illustration:

Scenario 1: Input:

col1 col2
1      a
1      b
2      c
2      d
3      e
3      f

Output:

col1 col2
1 a;b;
2 c;d;
3 e;f;

Scenario 2 Input:

col1 col2
1      a
2      c
3      e

Output:

 col1 col2

1 a;
2 c;
3 e;

I am trying to achieve the same using the below code. But not able to make my way around :

            String val='';
            for(Integer i=1; i<=fieldListInsert.size()-1; i++){
                if(fieldListInsert[i+1].col1!= fieldListInsert[i].col1){
                    updatelist.put(fieldListInsert[i].col1,val);
                    val='';
                }else{
                    val+=fieldListInsert[i].col2+';'+fieldListInsert[i+1].col2+';';
                }
            }

UPDATE: missed to mention fieldListInsert is the wrapper class and updatelist is a map of type map<Id,String> Since I couldn't explain from my data diagram I am adding image of input and required output

enter image description here

1

Instead of creating new list you should use map. Below is the code example:

map<Integer,list<String>> newKeyValuesMap= new map<Integer,list<String>>();
for(Integer i=0; i<=fieldListInsert.size(); i++){
    if(!newKeyValuesMap.containsKey(fieldListInsert[i].col1)){
     newKeyValuesMap.put(fieldListInsert[i].col1,new list<String>{fieldListInsert[i].col2});
    }
    else{
      newKeyValuesMap.get(fieldListInsert[i].col1).add(fieldListInsert[i].col2);
    }
}

You can learn more about maps here

below is the updated code version:

map<String,String> newKeyValuesMap= new map<String,String>();
for(Integer i=0; i<=fieldListInsert.size(); i++){ 
  if(newKeyValuesMap.containsKey(fieldListInsert[i].col1)){
   String val=newKeyValuesMap.get(fieldListInsert[i].col1)+fieldListInsert[i].col2+';';
   newKeyValuesMap.put(fieldListInsert[i].col1,val);
  }
  else{
     newKeyValuesMap.put(fieldListInsert[i].col1,fieldListInsert[i].col2+';');
  }
}

List<Wrapper> wrapperList=new List<Wrapper>();
for(String str : newKeyValuesMap.keySet()){
    Wrapper wrap =new Wrapper();
    wrap.col1=str;
    wrap.col2=newKeyValuesMap.get(str);
    wrapperList.add(wrap);
}
return wrapperList;
7
  • I tried your code but it is throwing me "Method does not exist or incorrect signature: void containsKey(String) from the type Map<Integer,List<String>>" on all the methods (get, put) used for map variable. Any idea why? I did declare the variable.
    – Akash
    May 21 at 13:57
  • Is col1 type of String? If it is, then please replace map<Integer,list<String>> with map<String,list<String>> May 21 at 13:59
  • Thanks that was the issue. But as I suspected looking at your code the output is not appending col2 values its just overall converting my wrapper into map
    – Akash
    May 21 at 14:14
  • So basically, what is happing here is, it is looping over your wrapper list and storing list of col2 for every unique col1 and then returning the map<String,List<String>>. map is just the way to return list of col2 values for every unique col1. May 21 at 14:29
  • That I understood but I have to append all the Map's List<String> as String separated by ';' .This I am unable to do. Example The output should be Like Map<String, String>.
    – Akash
    May 21 at 14:31
2

You need to use Maps and their feature of having distinct key

Example for your problem:

public with sharing class StackQuestion {

    public static Map<Integer, List<String>> processData(){
        List<Wrapper> wrap = new List<Wrapper>();
        Map<Integer, List<String>> mapToReturn = new Map<Integer, List<String>>();
        wrap.add(new Wrapper(1, 'a'));
        wrap.add(new Wrapper(1, 'b'));
        wrap.add(new Wrapper(2, 'c'));
        wrap.add(new Wrapper(2, 'd'));

        for(Wrapper wr : wrap) {
            if(mapToReturn.get(wr.col1) != null) {
                mapToReturn.get(wr.col1).add(wr.col2);
            } else {
                mapToReturn.put(wr.col1, wr.col2);
            }
        }

        return mapToReturn;

    }

    public class Wrapper {
        Integer col1;
        String col2;

        public Wrapper(Integer data1, String data2) {
            col1 = data1;
            col2 = data2;
        }
    }
}
2
  • Thanks but your code logic is same as @Shagun. The col2 append is not happening
    – Akash
    May 21 at 14:16
  • the append need to happen only for col2 where col 1 is the same so it's more like denormalizing 1NF in DBMS terms
    – Akash
    May 21 at 14:46
0

Here is an example, there is no error checking but it seems to fulfil your requirements:

public with sharing class StackQuestion {

private Map<Integer, Set<String>> valuesByIndex = new Map<Integer, Set<String>>();

public void addValue(Integer index, String value) {

    Set<String> values = getValues(index);

    if (values == null) {
        values = new Set<String>();
        valuesByIndex.put(index, values);
    }

    values.add(value);

}

public Set<String> getValues(Integer index) {
    return valuesByIndex.get(index);
}

public String getAllValuesForIndex(Integer index) {
    return String.valueOf(index) + ' ' + String.join(new List<String>(valuesByIndex.get(index)), ';');
}

}

And then the running code:

StackQuestion sq = new StackQuestion();
sq.addValue(1, 'a');
sq.addValue(1, 'b');
sq.addValue(2, 'c');
sq.addValue(2, 'd');
sq.addValue(3, 'e');
sq.addValue(3, 'f');
System.debug(sq.getAllValuesForIndex(1));
System.debug(sq.getAllValuesForIndex(2));
System.debug(sq.getAllValuesForIndex(3));

Which outputs:

|USER_DEBUG|[10]|DEBUG|1 a;b |USER_DEBUG|[11]|DEBUG|2 c;d |USER_DEBUG|[12]|DEBUG|3 e;f

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.