serendipity
serendipity

Reputation: 862

Comparing values of two maps for complete and partial matches

I have two maps with the same keys that could have values that are an exact match or a partial match. Based on these exact and partial matches I need to update my exact and partial match counters.

The maps contain values like:

{ORGANIZATION=[Orioles, Athletics]}
{ORGANIZATION=[Orioles, Kansas City Athletics]}

Here my counter for exact match should increment by 1 for Orioles and the one for partial match should increment by 1 for Kansas City Athletics. Right now the way I am doing it using map1.get(key).equals(map2.get(key)) and map1.get(key).contains(map2.get(key)) gives me incorrect values for my counters.

EDIT

Here's my code :

List<Integer> compareResults = new ArrayList<Integer>();  

               for (String key: annotationMap.keySet()){
                   if (key.equals("ORGANIZATION")){
                       if (annotationMap.get(key).equals(myMap.get(key))){
                           orgTruePositiveCount++;
                           continue;
                       }
                       if (annotationMap.get(key).contains(myMap.get(key))){
                           orgTruePositiveCount++;
                           continue;
                       }
                       if (!annotationMap.get(key).contains(myMap.get(key))){
                           orgFalseNegativeCount++;
                           continue;
                       }

                   }

                       if (key.equals("PERSON")){
                           if (annotationMap.get(key).equals(myMap.get(key))){
                               perTruePositiveCount++;
                               continue;
                           }
                           if (annotationMap.get(key).contains(myMap.get(key))){
                               perTruePositiveCount++;
                               continue;
                           }
                           if (!annotationMap.get(key).contains(myMap.get(key))){
                               perFalseNegativeCount++;
                               continue;
                           }

                       }

                           if (key.equals("LOCATION")){
                               if (annotationMap.get(key).equals(myMap.get(key))){
                                   locTruePositiveCount++;
                                   continue;
                               }
                               if (annotationMap.get(key).contains(myMap.get(key))){
                                   locTruePositiveCount++;
                                   continue;
                               }
                               if (!annotationMap.get(key).contains(myMap.get(key))){
                                    locFalseNegativeCount++;
                                    continue;
                               }

                            }
                        }

               for (String key: myMap.keySet()){
                   if (!annotationMap.containsValue(myMap.get(key))){
                       if (key.equals("ORGANIZATION")){
                           orgFalsePositiveCount++;
                       }

                   if (key.equals("PERSON")){   
                           perFalsePositiveCount++;
                       }

                   if (key.equals("LOCATION")){
                            locFalsePositiveCount++;
                   }   
                 }
               }

Upvotes: 0

Views: 1140

Answers (2)

alexdriedger
alexdriedger

Reputation: 3044

For exact matches:

Set<String> mapValues = map1.values();

int initialCount = mapValues.size();

return initialCount -  mapValues.removeAll(map2.values).size();

Upvotes: 0

Srikanth Balaji
Srikanth Balaji

Reputation: 2718

Possibly, you missed the either condition in case of partial match, try with the below.

public static void main(String[] args) throws Exception {
        int exactCounter = 0;
        int partialCounter = 0;
        Map<String, String> dataA = new HashMap<>();
        dataA.put("1", "123");
        dataA.put("2", "456");
        dataA.put("3", "7890");

        Map<String, String> dataB = new HashMap<>();
        dataB.put("1", "123");
        dataB.put("2", "456");
        dataB.put("3", "789");

        for (Map.Entry<String, String> entryA : dataA.entrySet())
        {
            if(entryA.getValue().equals(dataB.get(entryA.getKey()))){
                System.out.println("Exact Matched Key---"+entryA.getValue());
                exactCounter = exactCounter + 1;
            }
            else if(entryA.getValue().contains(dataB.get(entryA.getKey())) || dataB.get(entryA.getKey()).contains(entryA.getValue())){
                System.out.println("Partial Matched key---"+entryA.getKey());
                partialCounter = partialCounter + 1;
            }
        }
        System.out.println("Total Exact Match---"+exactCounter);
        System.out.println("Total Partial Match---"+partialCounter);

    }

Result -

Partial Matched key---3
Exact Matched Key---2
Exact Matched Key---1
Total Exact Match---2 Total Partial Match---1

Upvotes: 3

Related Questions