Question: I want to compare the keys of four hashmaps. All of the keys in the Hashmaps are equal. But I would like to evaluate each hashmap to compare the keys of all four hashmaps. In the code below, I am using 4 nested loops and then using a logical AND. If `true`, I want to add the Values to an Array List. Is this possible?

I have seen some code on the forum, but found nothing where we can compare 4 hashmaps. So do I need to add the Values to the ArrayList and not the Keys?

```
public static Map<String, Boolean> areEqualKeyValues(Map<String, String> firstMap, Map<String, String> secondMap,Map<String, String> thirdMap, Map<String, String> fourthMap) {

        ArrayList<String> reporValues = new ArrayList<>();
        HashMap<String, String> filteredMap = new HashMap<>();

        for (Map.Entry<String, String> entry1 : firstMap.entrySet()) {
            for (Map.Entry<String, String> entry2 : secondMap.entrySet()) {
                for (Map.Entry<String, String> entry3 : thirdMap.entrySet()) {
                    for (Map.Entry<String, String> entry4 : fourthMap.entrySet()) {
                            if (entry1.getKey().equals(entry2.getKey())
                                    && entry2.equals(entry3.getKey())
                                    && entry1.equals(entry3.getKey())
                                    && entry3.equals(entry4.getKey())) {
                                reporValues.add(entry1.getValue() + entry2.getValue() + entry3.getValue() + entry4.getValue());
                            }
                        }
                    }
                }
                filteredMap.put(entry1.getKey(), reporValues.toString());
            }

```

Answer: 
Try this.

```
public static Map<String, String> areEqualKeyValues(Map<String, String>... maps) {
    if (maps.length <= 0)
        throw new IllegalArgumentException("maps");
    Set<String> keys = new HashSet<>();
    for (Map<String, String> map : maps)
        keys.addAll(map.keySet());
    Map<String, String> filteredMap = new HashMap<>();
    for (String key : keys) {
        StringBuilder sb = new StringBuilder();
        for (Map<String, String> map : maps)
            sb.append(map.get(key));
        filteredMap.put(key, sb.toString());
    }
    return filteredMap;
}

```
You should not iterate over the `entrySet` but use the [`keySet`](https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#keySet()) instead.  

Then you can just use the [`retainAll`](https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#retainAll(java.util.Collection)) of the first `keySet` repeatedly to only retain the objects that are in all the sets.