你好,美女们,
我有一个用字符串键和列表定义Map的场景。现在,我希望将值逐个放入列表中,并使用单键显示它们。示例代码-
Map<string,List<Account>> accountMap = new Map<string,List<Account>>();
//How to insert values. I am using this but no luck-
for(Account acc = [Select Id, Name from Account]){
accountMap(acc.Id,accList)
}
//Lets say we have data. How to display any **specific** value from the middle of the list. I am using this but no luck-
AccountList.get(id).get(3);请帮帮忙。
发布于 2022-05-09 13:44:16
以帐户id为例是个糟糕的主意,因为它们将是唯一的,->,您的列表总是大小为0。但是的,你应该能做这样的事。
您的东西甚至不编译,您不能在for(Account acc = [SELECT...])中使用"=“。
假设你有账户,其中有些有重复的名字。这将接近你所需要的:
Map<string,List<Account>> accountMap = new Map<string,List<Account>>();
for(Account acc : [SELECT Name, Description FROM Account LIMIT 100]){
if(accountMap.containsKey(acc.Name)){
accountMap.get(acc.Name).add(acc);
} else {
accountMap.put(acc.Id, new List<Account>{acc});
}
}然后是的,如果你有密钥,你可以访问accountMap.get('Acme Inc.')[0].Id
https://stackoverflow.com/questions/72162974
复制相似问题