如何在 APEX 中将 SET 转换为数组?
我有带有键和值的地图,我的目标是获取“键”列表。 我正在考虑将其放入数组或列表中。 到了我在 SET 中有关键值但还没有弄清楚的地步 如何转换为数组。
下面是我的代码:
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 :(
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 :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一种快速而简单的方法是:
在我最近编写的一些代码中,我有一个
Set
并将其传递到一个采用List的方法中;
与methodName( new List(setVariable) );
或methodName(new String[setVariable] );
是的,我知道该帖子是 11+岁了......但这也是搜索时出现的问题,所以我把我的答案放在这里。
A quick and simple way to do this would also be:
In some code I wrote recently I've got a
Set<String>
and am passing it into a method that takes aList<String>
withmethodName( new List<String>(setVariable) );
ormethodName(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.
使用List.addAll方法?
http://www .salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_methods_system_list.htm?SearchType=Stem
如果没有 - 您始终可以手动循环遍历集合...
Use
List.addAll
method?http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_methods_system_list.htm?SearchType=Stem
If not - you could always manually loop through the set...
您可以使用:
Set keys = mmm.keySet();
列表 keyList = new List(keys);
Could you use:
Set keys = mmm.keySet();
List keyList = new List(keys);
为了类型安全,您应该始终使用泛型。
根据链接:https: //salesforce.stackexchange.com/questions/5447/is-there-a-difference- Between-an-array-and-a-list-in-apex 。
这个解决方案将会起作用。
You should always used generics for type safety.
As per link : https://salesforce.stackexchange.com/questions/5447/is-there-a-difference-between-an-array-and-a-list-in-apex .
This solution will work.