function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sweta kumari 55sweta kumari 55 

How to covert string to integer this list?

How to covert string to integer this list?
List<String> nameArray = new   List<String{'one','two','three','one','one'};

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

for(String key : nameArray)
{
    if(!elCount.containsKey(key)){
    elCount.put(key,0);
    }
    Integer currentInt=elCount.get(key)+1;
    elCount.put(key,currentInt);
}


output : 1=>3, 2=>1,3=>1
 
Prateek Prasoon 25Prateek Prasoon 25
Hey Sweta,

Here's how you can modify the code you provided to convert the String values in the nameArray list to Integer values:

List<String> nameArray = new List<String>{'one', 'two', 'three', 'one', 'one'};
Map<String, Integer> elCount = new Map<String, Integer>();

for (String key : nameArray) {
    if (!elCount.containsKey(key)) {
        elCount.put(key, 0);
    }
    Integer currentInt = elCount.get(key) + 1;
    elCount.put(key, currentInt);
}

// Print the output
for (String key : elCount.keySet()) {
    System.debug(key + "=>" + elCount.get(key));
}

If you find my answer helpful, please mark it as the best answer.