简体   繁体   English

检查arraylist java中存在的映射键

[英]Check map key present in arraylist java

Wanted to check if the keys of the hashmap is present in the arrayList.想要检查 hashmap 的键是否存在于 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;
    }

您可以使用Collection.containsAll()也归功于@OH GOD SPIDERS ,因为他们几乎在同一秒的评论中提到了它):

return list.containsAll(map.keySet());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM