0

I have two maps that look like this:

{PERSON=[Jack Fisher], ORGANIZATION=[Oriole, American League]}
{LOCATION=[Oriole], PERSON=[Jack Fisher]}

I need to compare them and increment some counters for an Exact Match,Partial Match and No Match. I am iterating over these two maps and their values but since they are not in order my counts are all messed up. Is it possible to compare these maps? If not then is there a workaround where I put them in order and then compare. And how do I put them in exact order?

EDIT - ADDING CODE

private static List<Integer> compareMaps(LinkedHashMap<String, List<String>> annotationMap,
        LinkedHashMap<String, List<String>> rageMap) {
    List<Integer> compareResults = new ArrayList<Integer>();
    int i_index = 0;
    for (Entry<String, List<String>> entry : annotationMap.entrySet()) {
        if (rageMap.entrySet().isEmpty()) {
            orgFalseNegativeCount++;
            continue;
        }
        for (Entry<String, List<String>> rageEntry : rageMap.entrySet()) {

            if (entry.getKey().equals("ORGANIZATION")) {
                for (String val : entry.getValue()) {
                    recusion: for (int i = i_index; i < rageEntry.getValue().size();) {
                        String rageVal = rageEntry.getValue().get(i);
                        if (val.equals(rageVal)) {
                            orgTruePositiveCount++;
                            i_index++;
                            break recusion;
                        }

                        else if ((val.length() > rageVal.length()) && val.contains(rageVal)) { // ||
                                                                                                // dataB.get(entryA.getKey()).contains(entryA.getValue())){
                            orgTruePositiveCount++;
                            i_index++;
                            break recusion;
                        } else if ((val.length() < rageVal.length()) && rageVal.contains(val)) {
                            orgTruePositiveCount++;
                            i_index++;
                            break recusion;
                        }

                        else if (!val.contains(rageVal)) {
                            orgFalseNegativeCount++;
                            i_index++;
                            break recusion;
                        } else if (!rageVal.contains(val)) {
                            orgFalsePositiveCount++;
                            i_index++;
                            break recusion;
                        }

                    }
                }
            }

            if (entry.getKey().equals("PERSON")) {
                for (String val : entry.getValue()) {
                    recusion: for (int i = i_index; i < rageEntry.getValue().size();) {
                        String rageVal = rageEntry.getValue().get(i);

                        if (val.equals(rageVal)) {
                            perTruePositiveCount++;
                            i_index++;
                            break recusion;
                        }

                        else if ((val.length() > rageVal.length()) && val.contains(rageVal)) {
                            perTruePositiveCount++;
                            i_index++;
                            break recusion;
                        }

                        else if ((val.length() < rageVal.length()) && rageVal.contains(val)) {
                            perTruePositiveCount++;
                            i_index++;
                            break recusion;
                        }

                        else if (!val.contains(rageVal)) {
                            perFalseNegativeCount++;
                            i_index++;
                            break recusion;
                        }

                        else if (!rageVal.contains(val)) {
                            perFalsePositiveCount++;
                            i_index++;
                            break recusion;
                        }

                    }
                }
            }
        }
    }
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
serendipity
  • 852
  • 13
  • 32
  • 1
    Iterate over the entries of one Map and use otherMap.get(entry.getKey()) to obtain the corresponding value from the other Map (if exists). Also check that the sizes of both Maps are equal. – Eran Feb 12 '17 at 13:29
  • Can you share the output you expect for this input? It will help us understand the question better. – Mureinik Feb 12 '17 at 13:32
  • @Mureinik Edited question to add my code. Thanks – serendipity Feb 12 '17 at 13:38

1 Answers1

0

You may use SortedMap<K,V> and TreeMap<K,V> like that:

SortedMap<String, List<String>> left  = new TreeMap<>();
SortedMap<String, List<String>> right = new TreeMap<>();
left .addAll( annotationMap );
right.addAll( rageMap );
compare( left, right );

To compare the Lists you may use this answer.

Community
  • 1
  • 1
Aubin
  • 14,617
  • 9
  • 61
  • 84