Loop on maps to find duplicate My code does the following:

There are two  Maps with properties. I need to check if properties with the same key and value already exist, and if they do, throw an exception.

    Map&lt;String, Object&gt; existingProperties = requires.get(requiresIndex).getProperties();
    for (Map.Entry&lt;String, Object&gt; property : properties.entrySet()) {
        if (existingProperties.containsKey(property.getKey())){
            if (existingProperties.get(property.getKey()) instanceof String &amp;&amp; property.getValue() instanceof String ){
                String existingValue = (String) existingProperties.get(property.getKey());
                String newValue = (String) property.getValue();
                if (existingValue.equals(newValue)){
                    throw new Exception("Property " + property.getKey() + " is existsing with value " + newValue + "in requires " + requiresName);
                }
            }
        }
    }


We are working on Java 8. Is there a better/shorter way to write it?
Sharon Ben Asher has a good answer using streams. I'd like to add some suggestions for the original code, since beginners are often not familiar with streams in Java.

You have some redundancy in your code, meaning code that is executed multiple times, which can hurt readability, decrease performance and introduce bugs. To tackle the issue, you can save results from method calls in variables and just access the variables multiple times. This also allows you to give the result a new name, and although it might make the code slightly longer in terms of lines of code, it can also make it a lot cleaner, and the individual lines shorter.

The most important example is calling property.getKey() and property.getValue() multiple times. Instead, do that at the beginning of the loop, and save the results in variables.

for (Map.Entry&lt;String, Object&gt; property : properties.entrySet()) {
    String key = property.getKey();
    Object value = property.getValue();


Then you access them in your code, which makes it easier to see where the same values are used. You are using the same method call to access another element of a different map, so again you can use a variable to save the result and make the code clearer and shorter:

    if (existingProperties.containsKey(key)) {
        Object existingObject = existingProperties.get(key);
        if (existingObject instanceof String &amp;&amp; value instanceof String) {
            String existingValue = (String) existingObject;
            String newValue = (String) value;


We can improve readability by formatting very long lines (like that long string concatenation) on 2 or 3 lines, introducing newlines where we can devide it into logical parts:

            if (existingValue.equals(newValue)) {
                throw new Exception("Property " + key +
                                    " is existing with value " + newValue +
                                    " in requires " + requiresName);
            }


By the way, there seems to have been a space missing at the start of the string "in requires ", and a typo in " is existsing with value ".