You need to sign in to do that
Don't have an account?
vikrant kumar
need to transfer values into map
I have to move values with in brackets of string s into map having s1,s3,s4 as keys and 40,98,78 as values.how to do that?
i am trying the following code in workbench.
string s='(s1,40),(s3,98),(s4,78)';
list<string> k=s.replace(')','').replace('(','').split(',');
map<string,string> maps=new map<string,string>();
for(integer i=0;i<k.size();i++){
maps.put('k[i]','k[i++]');
}
system.debug('size of map---------'+ maps.size());
Is there any better way to do this?please let me know?
In workbench the size of map shows 1 when executed.But it is 3.is some thing wrong with my logic.
i am trying the following code in workbench.
string s='(s1,40),(s3,98),(s4,78)';
list<string> k=s.replace(')','').replace('(','').split(',');
map<string,string> maps=new map<string,string>();
for(integer i=0;i<k.size();i++){
maps.put('k[i]','k[i++]');
}
system.debug('size of map---------'+ maps.size());
Is there any better way to do this?please let me know?
In workbench the size of map shows 1 when executed.But it is 3.is some thing wrong with my logic.
All Answers
There is a mistake in your code. Please try below, it works fine:
String s='(s1,40),(s3,98),(s4,78)';
List<String> k=s.replace(')','').replace('(','').split(',');
Map<String, String> maps=new map<String, String>();
for(integer i=0;i<k.size();i++)
{
maps.put(k[i], k[i+1]);
i++;
}
system.debug('size of map---------'+ maps.size());// It will give 3 now.
Please let me know if it helps.
Thanks and Regards,
Anirudh Singh