5

I'm wondering the best way to compare these two HashMaps. I want to verify if they are the same, and if not, what is the difference. If it matters, then I'm wondering what the 2nd one has/does not have that the first hashmap does have. I'll need to know if one has a key the other does not, as well as the Value List differences per key. I'm hoping there is a simple way to map this, but not sure. Basic Example:

    HashMap<String, List<String>> hmOne = new HashMap<String, List<String>>();
    List<String>l1 = new ArrayList<String>();

    l1.add("one");
    l1.add("two");
    l1.add("three");
    l1.add("four");
    l1.add("five");
    hmOne.put("firstkey", l1);
    l1 = new ArrayList<String>();

    l1.add("1");
    l1.add("2");
    l1.add("3");
    l1.add("4");
    l1.add("5");
    hmOne.put("secondkey", l1);

    HashMap<String, List<String>> hmTwo = new HashMap<String, List<String>>();
    List<String>l2 = new ArrayList<String>();
    l2.add("one");
    l2.add("two");
    l2.add("four");
    l2.add("five");
    hmTwo.put("firstkey", l2);
    l2 = new ArrayList<String>();

    l2.add("1");
    l2.add("3");
    l2.add("4");
    l2.add("5");
    hmTwo.put("secondkey", l2);

Thanks for any help.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
Green
  • 1,405
  • 4
  • 21
  • 27
  • 1
    http://stackoverflow.com/questions/3387155/difference-between-two-maps/3387383#3387383 – jcbelanger Oct 19 '11 at 19:02
  • 1
    I wrote my own comparison. I pass in the two hashmaps to compare and I get back what the first has that the second does not, and vice versa. – Green Oct 20 '11 at 19:50
  • @Green i know it has been some time, but could you..accept an answer or post your own solution as an answer (prefered)? greetings – Gewure Sep 20 '17 at 14:02

4 Answers4

6

guava has Maps.difference(map1, map2)

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 1
    Guava also has `Lists.newArrayList("1", "2", "3", "4", "5")`, which will make the original code much shorter than it is right now. – Roland Illig Oct 19 '11 at 18:56
2

HashMap.equals will tell you if they are identical (same keys and values) but the rest you will have to roll yourself.

You will need to iterate the keyset() of one HashMap, look for it in the keySet() of the other and if found then compare the values.

Then, you will have to do the reverse, looking for keys in the second that don't exist in the first. You can probably use Set methods for this.

Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
0

You know that Map's .equals() method will tell you if two Maps are equal, right?

If they aren't equal, you're going to have to parse them both and figure out the differences on your own.

miken32
  • 42,008
  • 16
  • 111
  • 154
Bob Kuhar
  • 10,838
  • 11
  • 62
  • 115
0

Off the top of my head, you could first use HashMap.equals() to tell if they are different and then get the keySet of each hashmap and compare them:

What is the fastest way to compare two sets in Java?

Then once you've got the differences in keys, you could repeat the process on your value collections.

Community
  • 1
  • 1
SuperTron
  • 4,203
  • 6
  • 35
  • 62