Comparing two Collections in Java

前端 未结 7 1472
孤独总比滥情好
孤独总比滥情好 2020-12-15 23:30

I have two Collections in a Java class.The first collection contains previous data, the second contains updated data from the previous collection.

I would like to c

相关标签:
7条回答
  • 2020-12-16 00:36
    • Iterate over the first collection and add it into a Map<Entity, Integer> whereby Entity is the class being stored in your collection and the Integer represents the number of times it occurs.
    • Iterate over the second collection and, for each element attempt to look it up in the Map - If it exists then decrement the Integer value by one and perform any action necessary when a match is found. If the Integer value has reached zero then remove the (Entity, Integer) entry from the map.

    This algorithm will run in linear time assuming you've implemented an efficient hashCode() method.

    0 讨论(0)
提交回复
热议问题