I have map with key and value and my goal is to get list of 'key'. I am thinking to get it to the array or List. Got to the point where I have key values in the SET but haven't figure out how to convert to the array.
below is my code:
Map<String, String> mmm = new Map<String, String>();
mmm.put('one', 'oneee');
mmm.put('two', 'twooo');
mmm.put('three', 'threeee');
mmm.put('four', 'fourff');
//outputs values in the map
system.debug('=======values()==========>' + mmm.values());
//outputs key in the map
system.debug('=======keyset()===========>' + mmm.keyset());
//get keys in the type SET
SET<string> s = mmm.keyset();
//returns 4
system.debug('------------------------------------' + s.size());
s.arrayTo() //this method does not exist :( 1 4 Answers
Use List.addAll method?
If not - you could always manually loop through the set...
2Could you use:
Set keys = mmm.keySet(); List keyList = new List(keys);
You should always used generics for type safety.
Map<String, String> mmm = new Map<String, String>();
mmm.put('one', 'oneee');
mmm.put('two', 'twooo');
mmm.put('three', 'threeee');
mmm.put('four', 'fourff');
List<String> lstKeys = new List<String>(mmm.keyset());
System.debug('Output : '+lstKeys);As per link : .
This solution will work.
A quick and simple way to do this would also be:
new List<String>(mmm.keySet());
//or
new String[mmm.keySet()];In some code I wrote recently I've got a Set<String> and am passing it into a method that takes a List<String> with methodName( new List<String>(setVariable) ); or methodName(new String[setVariable] );
Yes I know the post is 11+ years old... but it is also what comes up when searching so I put my answer here.