public ClassA { private String firstId; private String secondId; public void setFirstId(String firstId) { this.firstId = firstId; } public String
With the default implementation of Object.equals, two objects of different classes will never be the same.
If you want that behavior, you will need to override .equals in your classes (and .hashcode as well, look at the javadoc).
(Or make a comparator and pass it to your collections).