Search code examples
javaobjectdeep-copy

.contains() on a list of objects


I deep copied a list of objects into another list. The problem is using .contains() didn't work! Any ideas?


Solution

  • In order for this to work you need to override hashcode and equals on the contents of the list. Contains uses these methods, which by default are based on the memory reference so it will only work for a shallow copy.