How to compare two maps and retrieve key of the value that occurs in both maps?

Sanjay Bharathi :
newPropertiesFile.keySet().parallelStream()
    .filter(value -> oldPropertiesFile.keySet().parallelStream()
            .filter(entry -> oldPropertiesFile.get(entry).toString().equals(newPropertiesFile.get(value).toString()))
            .filter(values -> !values.equals(value)).count() > 0)
    .collect(Collectors.toMap(entryKey -> (String) entryKey, entryKey -> newPropertiesFile.get(entryKey).toString()));

For Example, I have mapA = {(1,'a'),(2,'b'),(3,'c')} and mapB = {(5,'a'),(6,'d'),(7,'c')} Comparing the valueList of both the maps, the values 'a' and 'c' in mapA occur in mapB and their keys are 5 and 7 resp.

And hence my required o/p:
5, 7

I have done the above and got my required output. But the Complexity is too damn high on O(n^2). Any optimized methods?

A more Simplified example:

mapA.keySet().parallelStream()
    .filter(v->mapB.keySet().parallelStream()
            .filter(e->mapB.get(v).equals(mapA.get(v)))
            .filter(v->!v.equals(v)).count()>0)
    .forEach(System.out::println);
Eritrean :

If I get this right:

Comparing the valueList of both the maps, the values 'a' and 'c' in mapA occur in mapB and their keys are 5 and 7 resp. And hence my required o/p: 5, 7

Isn't it enough just to filter your second map with list#contains:

    Map<Integer,String> mapA = new HashMap<>();
    mapA.put(1, "a");
    mapA.put(2, "b");
    mapA.put(3, "c");
    Map<Integer,String> mapB = new HashMap<>();
    mapB.put(5, "a");
    mapB.put(6, "d");
    mapB.put(7, "c");

    List<Integer> list = mapB.entrySet().stream()
                             .filter(e->mapA.containsValue(e.getValue()))
                             .map(e -> e.getKey())
                             .collect(Collectors.toList());
    System.out.println(list);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=328707&siteId=1