0

I have map which size is 10.from the Map from each 3 keys i have to form a string. then the string is set to empty.from the next (3 to 6) map keys again i want to form a string. Please guide me

Apex class

String strval='';

Integer incrementer=0;


for(String strId:mapVal.keyset()){

    incrementer++
    if(strval== ''){
             strval= '\'' + strId+ '\'';
    } else {
             strval+= ',\'' + strId+ '\'';
    }   


    if(incrementer==3){
        system.debug('strval******'+strval);
        break;
    }
}

Eg

mapVal={01='apple'},{02='banaa'},{03='mango'},{04='cat'},{05='dog'}

for the first time if map size until 3 i want like this

strval=applebanaamango

After that

strval=catdog
0

You can use the following code. Result of this concatenation would be return by method.

private List<String> getConcatanetedValues(Map<String, String> mapToProcess, Integer count){
    List<String> result = new List<String>();
    String tempValue = '';
    for(Integer i=0; i<mapToProcess.values().size(); i++){
        String value = mapToProcess.values().get(i);
        tempValue+=value;
        if(Math.mod(i, count) == count-1) {
            result.add(tempValue);
            tempValue = '';
        }
    }
    if(String.isNotBlank(tempValue)){
        result.add(tempValue);
    }
    return result;
}

Usage:

Map<String, String> mapValues = new Map<String, String>{
    '01' => 'apple',
    '02' => 'banana',
    '03' => 'mango',
    '04' => 'cat',
    '05' => 'dog'
};
System.debug(getConcatanetedValues(mapValues, 3));

//DEBUG|(applebananamango, catdog)

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.