map可以与java中的字符串数组列表进行比较
private Map<String, String> checkInScopeLobs(Map<String, String> allLobsChkBx)
{
Map<String, String> inScopeLobs = new HashMap<String, String>();;
for (Map.Entry<String, String> entry : allLobsChkBx.entrySet())
{
if(entry.getKey().contains("1") || entry.getKey().contains("2") || entry.getKey().contains("3")){
inScopeLobs.put(entry.getKey(), entry.getValue());
}
}
return inScopeLobs;
}
这是正确的方法吗?
最佳答案
您可以使用keySet()
。此方法返回键的Set
(有关详细信息, Docs from Oracle about Map )。这意味着比迭代整个 map 的开销更少。在以下情况下,您将仅请求匹配键的值。
还有一些其他错误,例如双分号,并且从 JDK7 开始,您不必在初始化时定义映射。
private Map<String, String> checkInScopeLobs(Map<String, String> allLobsChkBx) {
Map<String, String> inScopeLobs = new HashMap();
List<String> keys = Arrays.asList( { "1", "2", "3" } );
for(String key : allLobsChkBx.keySet()) {
if(keys.contains(key)) {
inScopeLobs.put(key, allLobsChkBx.get(key));
}
}
return inScopeLobs;
}
为什么不使用Integer
而不是String
,因为您只存储数字。
关于java - 将映射键与字符串列表进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16413143/