-2

I have a Map with key as String and value as List as below

<Key>Path1 Value=[164,123,111,131]

<Key>Path2 Value=[164,122,135,133]

<Key>Path3 Value=[190,144,100,126]

<Key>Path4 Value=[113,122,453,117]

I want to compare each Key's Value with other Key's Value like Path1 Value with rest of Path's values and so on,

and also no duplicate comparision should happen, like if Path1 value is compared in 1st iteration . It should not compare Path2 with Path1 in 2nd iteration.

Am stuck with this problem . Please help me with any solution. Thanks in advance .

I have started with following code :

for (Map.Entry<String, List<String>> entry : map1.entrySet()) {
        String key = entry.getKey();
        for (String val : entry.getValue()) {
            // do something with key and each val


        }
    }
  • Please format your question so it's easier to make sense of – Carpetfizz May 07 '17 at 19:22
  • What do you mean by comparing arrays? What makes one array greater than the other? Or by compare do you want to see if they are the same ? – Carpetfizz May 07 '17 at 19:25
  • This is an request for getting code done, please provide what you got so far if you have even started anything. – FilipRistic May 07 '17 at 19:26
  • Values is List of String. Yes i want to compare if there is any single element common in corresponding List . – Vijay Patil May 07 '17 at 19:28
  • See this question: http://stackoverflow.com/questions/1128723/how-can-i-test-if-an-array-contains-a-certain-value?rq=1 – Ivan Pronin May 07 '17 at 19:28
  • If you mean that something should be done by 164=164, that is a misinterpretation of how HashMap works. Actually it gets the hash value ([Object.hashCode](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode()), a simple number) once, and uses that value for finding storage for each element, but that is not sorting. If you want something ordered by a key, check [TreeMap](https://docs.oracle.com/javase/7/docs/api/java/util/TreeMap.html), and provide your own Comparator/Comparable. – tevemadar May 07 '17 at 19:32

2 Answers2

1

Only compare keys where the first is less than the second, or some similar simple strategy.

for (String key1 : map.keySet()) {
    for (String key2 : map.keySet()) {
        if (key1.compareTo(key2) < 0) {
            // compare key1 to key2
        }
    }
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

Maybe, this would be a better strategy than one suggested by Peter Lawrey cause it's not O(N*N)

    Map<String, List<String>> map = new HashMap<>();

    map.put("Path1", new ArrayList<>(Arrays.asList("164","123","111","131")));
    map.put("Path2", new ArrayList<>(Arrays.asList("164","122","135","133")));
    map.put("Path3", new ArrayList<>(Arrays.asList("190","144","100","126")));
    map.put("Path4", new ArrayList<>(Arrays.asList("113","122","453","117")));

    List<String> list = new LinkedList<>(map.keySet());

    for (int i = 0; i < list.size(); i++) {
        for (int j = i + 1; j < list.size(); j++) {
            List<String> temp = new ArrayList<>(map.get(list.get(i)));
            if (temp.removeAll(map.get(list.get(j)))) {
                // do what you want
                System.out.println(list.get(i) + " has duplicates with " + list.get(j));
            }
        }
    }
Yuri H
  • 680
  • 5
  • 12