1

Is possible to compare many maps stored in an arrayList?

I mean:

public static void main(String[] args) {
    List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
    
    Map<String, Object> entry1  = new HashMap<>();
    Map<String, Object> entry2  = new HashMap<>();
    Map<String, Object> entry3  = new HashMap<>();
    Map<String, Object> entry4  = new HashMap<>();
    
    entry1.put("entity1", "19820271");
    entry2.put("entity1", "19820271");
    entry3.put("entity2", "19820272");
    entry4.put("entity2", "19820272");
    
    mapList.add(entry1);
    mapList.add(entry2);
    mapList.add(entry3);
    mapList.add(entry4);
    
    groupMaps(mapList);
    
}

private static List<Map<String, Object>> groupMaps(List<Map<String, Object>> mapList) {
    List<Map<String, Object>> resultante = new ArrayList<>();
    
    for (int i = 0; i < mapList.size(); i++) {
        Map<String, Object> map1= mapList.get(0);
        for (Map.Entry<String, Object> entry : mapList.get(i).entrySet()) {
            if (entry.getValue().equals(map1.get("entity"))) {
                resultante.add(mapList.get(i));
            }
        }
    }
    return mapList;
}

In the "groupMaps" method I need to compare each map so I can group by match, for example:

map1 matches map2

map3 matches map7

map4 matches map5

and so on...

Is there any way to do it?

2
  • 1
    You want to find the duplicated maps (that have completely identical entries) and store them into a separate list, right? Commented Feb 21, 2022 at 18:22
  • @AlexanderIvanchenko yes, you're right. Commented Feb 21, 2022 at 18:30

1 Answer 1

1

The most performant way to do that is to create a histogram of frequencies. I.e. a map that will use your maps from the source list as keys and values will represent the number of occurrences of each map in the list.

That is how it could be implemented using Stream API.

private static List<Map<String, Object>> groupMaps(List<Map<String, Object>> mapList) {
    return mapList.stream()
                .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
                .entrySet().stream()
                .filter(entry -> entry.getValue() > 1)
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
}

The same result can be achieved using an iterative approach. The main logic remains the same:

  • create a map of frequencies;
  • then extract duplicates based on it.
    private static List<Map<String, Object>> groupMaps(List<Map<String, Object>> mapList) {
        Map<Map<String, Object>, Integer> histogram = getHist(mapList);
        List<Map<String, Object>> duplicates = new ArrayList<>();
        for (Map.Entry<Map<String, Object>, Integer> entry: histogram.entrySet()) {
            if (entry.getValue() > 1) {
                duplicates.add(entry.getKey());
            }
        }
        return duplicates;
    }

    private static Map<Map<String, Object>, Integer> getHist(List<Map<String, Object>> mapList) {
        Map<Map<String, Object>, Integer> histogram = new HashMap<>();
        for (Map<String, Object> next: mapList) {
            histogram.merge(next, 1, Integer::sum);
        }
        return histogram;
    }

Output for your example (both versions)

[{entity2=19820272}, {entity1=19820271}]

Note, if you are working on this problem just for exercise it's OK, but you should be aware that using Object as a generic type is as good as not using generics at all. If your map is intended to store string values then it has to be Map<String, String>.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.