You need to sign in to do that
Don't have an account?
sweta 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
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
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.