简体   繁体   English

检查地图中存在的值列表的最佳方法

[英]Best way to check a list of values present in a map

I have a Collection containing some values. 我有一个包含一些值的Collection

Collection<String> myList = new ArrayList<String>();
myList.add("a");
myList.add("b");
myList.add("c");

I created a Map which has some values: 我创建了一个具有一些值的Map

Map<String, String> myMap = new HashMap<String, String>();
myMap.put("r", "r");
myMap.put("s","m");
myMap.put("t", "n");
myMap.put("a", "o");

I want to check whether the values in the list are present as a key in the map? 我想检查列表中的值是否作为键出现在地图中? The way which I know using Java is to iterate through the list and check if the map contains that particular value using myMap.containsKey(). 我知道使用Java的方式是遍历列表,并使用myMap.containsKey()检查地图是否包含该特定值。 I want to know if there is anything out there using streams or for-each which saves me either lines of code or an efficient way of doing that! 我想知道是否有使用流或for-each的东西,这可以节省代码行或执行该操作的有效方式! Thanks for thinking through it. 感谢您的仔细考虑。

EDIT: I want to store all the elements present in the myList which are not there in myMap . 编辑:我想存储myList中不存在的myMap存在的所有元素。 So my output can be a list eg in this scenario [b,c] . 所以我的输出可以是一个列表,例如在这种情况下[b,c]

Set<String> mySet = new HashSet<String>(myList);
myMap.keySet().containsAll(set);

Does this answer your question? 这回答了你的问题了吗?

After your EDIT: Given the above, you can get the keys difference between two sets using the answer to one of these questions:- What is the best way get the symmetric difference between two sets in java? 编辑后:鉴于以上所述,您可以使用以下问题之一的答案来获得两组之间的键差:- 在Java中获得两组之间的对称差的最佳方法是什么? or Getting the difference between two sets 得到两组之间的差异

...whether the values in the list are present as a key in the map? ...列表中的值是否在地图中作为键出现?

As I understand this case as you need to verify whether all the elements in the myList are present as the myMap 's keys. 据我了解这种情况,您需要验证myList中的所有元素是否作为myMap的键存在。 Use the Stream::allMatch method: 使用Stream::allMatch方法:

myMap.keySet().stream()
              .allMatch(myList::contains);

... which is the same as: ...等同于:

myList.keySet().containsAll(myMap);

EDIT: I want to store all the elements present in the myList which are not there in myMap . 编辑:我想存储myList中不存在的myMap存在的所有元素。 So my output can be a list eg in this scenario [b,c]. 所以我的输出可以是一个列表,例如在这种情况下[b,c]。

You don't want to store all the elements in the myList since they are already there filled. 您不希望将所有元素存储在myList因为它们已经在此处填充。 You want to retain those elements using List::retainAll . 您想使用List::retainAll 保留那些元素。

myList.retainAll(myMap.keySet());

Printing out the result of myList would produce only the keys , that have been found in the original myList and also as a key of myMap . 打印出myList的结果将生成在原始myList找到的键 ,也将其作为myMap的键。

For: I want to store all the elements present in the myList which are not there in myMap. 对于:我想存储myList中不存在的所有元素。 So my output can be a list eg in this scenario [b,c] 因此,我的输出可以是一个列表,例如在这种情况下[b,c]

Create a duplicate myList and remove all myMap's keys 创建一个重复的myList并删除所有myMap的键

        Collection<String> newList = new ArrayList<>(myList);
        newList.removeAll(myMap.keySet());
        System.out.println(newList);

Using Streams you can do something like: 使用流,您可以执行以下操作:

List<String> result = myList.stream()                
                            .filter(myMap::containsKey)
                            .collect(Collectors.toList());

The resulting list will contain the values that are present as key in the map. 结果列表将包含在映射中作为键出现的值。

In case you want to print them out you can do: 如果要打印出来,可以执行以下操作:

result.forEach(System.out::println);

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

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