检查ArrayList Java中存在的地图密钥
想检查阵列列表中的hashmap的键是否存在。
尝试了下面的操作,它可以正常工作,但想知道是否还有其他更好的解决方案。
public boolean check(Map<String, String> map) {
Collection<String> list = new ArrayList<String>();
list.add("One");
list.add("Two");
list.add("Three");
for(String key: map.keySet()) {
if(!list.contains(key)) {
return false;
}
}
return true;
}
Wanted to check if the keys of the hashmap is present in the arrayList.
Tried the below and it working, but wanted to know if there are any other better solution.
public boolean check(Map<String, String> map) {
Collection<String> list = new ArrayList<String>();
list.add("One");
list.add("Two");
list.add("Three");
for(String key: map.keySet()) {
if(!list.contains(key)) {
return false;
}
}
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
collection.containsall()
(也将@oh上帝蜘蛛归功于他们在几乎在同一第二个评论中提到的):You can use
Collection.containsAll()
(also credits to @OH GOD SPIDERS since they mentioned it in the comments almost in the same second) :