我设置了一个测试:
- 检索有关多个法院案件的数据:每个法院案件都存储在 CourtCase 对象中
- 然后将一组 CourtCase 对象存储在 Map 中
- 我两次检索这些数据(从两个不同的来源),因此最终得到了两张 map
Map 之间对象内的数据应该相同,但 Map 内对象的顺序可能不同:
map 1: A、案例1 - B、案例2 - C、案例3
map 2: B、案例2 - A、案例1 - C、案例3
如何最好地比较这两张 map ?
最佳答案
Map#equals
不关心顺序。只要您的 2 个映射包含相同的映射,它就会返回 true。
Map#equals
使用 Set#equals
方法,应用于条目集。 Set#equals
contract :
Returns true if the specified object is also a set, the two sets have the same size, and every member of the specified set is contained in this set (or equivalently, every member of this set is contained in the specified set).
注意:这假设您的 CourtCase
对象具有要比较的正确 equals
和 hashcode
方法。
关于java - 比较对象图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11937336/