Comparing two maps

后端 未结 2 884
萌比男神i
萌比男神i 2020-12-02 15:32

I have two maps declared as Map. The Object here could be another Map (and so on). I want

2条回答
  •  执念已碎
    2020-12-02 15:41

    As long as you override equals() on each key and value contained in the map, then m1.equals(m2) should be reliable to check for maps equality.

    The same result can be obtained also by comparing toString() of each map as you suggested, but using equals() is a more intuitive approach.

    May not be your specific situation, but if you store arrays in the map, may be a little tricky, because they must be compared value by value, or using Arrays.equals(). More details about this see here.

提交回复
热议问题