Object comparison for equality : JAVA

前端 未结 4 1104
野性不改
野性不改 2020-12-21 14:20
public ClassA
{
private String firstId;
private String secondId;

public void setFirstId(String firstId) {
           this.firstId = firstId;
   }


   public String         


        
4条回答
  •  情深已故
    2020-12-21 14:43

    You can implement a compareTo(ClassB) or even a compareTo(CommonInterfaceOrSuperclass) method in each that will do the field-level comparison and return true if they meet your comparison criteria. You would then need to iterate over your lists and check against each element.

    Another alternative is to implement the equals()/hashcode() pair, which would, when coded properly, allow your List to find and compare for equals-ness successfully. Your contains() test would then work.

提交回复
热议问题