简体   繁体   English

比较键与值列表中的Java

[英]Compare keys with llist of values Java

Assuming the TreeMap<String,List> one and its copy as bellow, i want to compare all keys in the first one with all values in the second one. 假设TreeMap<String,List> one及其下面的副本,我想将第一个中的所有键与第二个中的所有值进行比较。 If a key has no match in values, as AUF_1060589919844_59496 and AUF_1421272434570_1781 in this case, i want to get the key and its values back. 如果某个键的值不匹配,例如AUF_1060589919844_59496和AUF_1421272434570_1781在这种情况下,我想找回该键及其值。

{AUF_1060589919844_59496=[AUF_1086686287581_9999,
AUF_1086686329972_10049, AUF_1079023138936_6682], 
AUF_1087981634453_7022=[AUF_1421268533080_1741, AUF_1421268568003_1743],
AUF_1421268533080_1741=[AUF_1421268719761_1776], 
AUF_1421272434570_1781=[AUF_1087981634453_7022]}

copy of above 上面的副本

    {AUF_1060589919844_59496=[AUF_1086686287581_9999,
AUF_1086686329972_10049, AUF_1079023138936_6682], 
AUF_1087981634453_7022=[AUF_1421268533080_1741, AUF_1421268568003_1743],
AUF_1421268533080_1741=[AUF_1421268719761_1776], 
AUF_1421272434570_1781=[AUF_1087981634453_7022]}

What I understand from your problem is to get key which are not there in values and its value also. 我从您的问题中了解到的是获取不存在于值中的键,并且其值也存在。 I think there is no need to create copy of it. 我认为没有必要创建它的副本。 I am posting a code snippet, I think this will certainly help you 我正在发布代码段,我认为这一定会对您有所帮助

Map<String, List<String>> map = new HashMap<String, List<String>>(); //Add elements in map Collection<List<String>> list = map.values(); List<String> values = new ArrayList<String>(); for (List<String> listValues : list) { values.addAll(listValues); } for (String key : map.keySet()) { if (!values.contains(key)) { System.out.println("key ---->" + key); System.out.println("Values ------->"); for (String value : map.get(key)) { System.out.println(value); } } }

If my assumption is correct you want all the keys that are not values; 如果我的假设是正确的,那么您希望所有不是值的键;

well this is very dirty way of doing it. 好吧,这是非常肮脏的方式。

Set<String> keys= new HashSet<String>(one.keySet());  //ensure we don't mess up with the actual keys in the Map

for(List list : one.values()){
 keys.removeAll(list);  //remove all those Keys that are in values
}

// print out keys that are not values
System.out.println(keys);

Using set will make life easy, as it doesn't contain duplicates, and we can remove values very quicky (using removeAll() method) 使用set将使生活变得轻松,因为它不包含重复项,并且我们可以非常快地删除值(使用removeAll()方法)

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

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