您需要equals
在类中实现方法.
类Object的equals方法实现了对象上最具辨别力的等价关系; 也就是说,对于任何非空引用值x和y,当且仅当x和y引用同一对象时,此方法才返回true(x == y的值为true).
添加以下方法将对您有用.
@Override public boolean equals(Object o) { // If the object is compared with itself then return true if (o == this) { return true; } /* Check if o is an instance of Complex or not "null instanceof [type]" also returns false */ if (!(o instanceof Employee)) { return false; } // typecast o to Complex so that we can compare data members Employee c = (Employee) o; // Compare the data members and return accordingly return (rollno == c.rollno && Objects.equals(name, c.name)) }