Search code examples
javacomparisonkey-valuetreemap

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. 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_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]}

Solution

  • 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); } } }