Verify there is a combination of unique string

前端 未结 4 1903
忘了有多久
忘了有多久 2021-01-21 12:17
class Details{
 String name;
 String age;
 String email;
 String location;
}

1) If there is List of Details as in List

how
4条回答
  •  春和景丽
    2021-01-21 12:40

    If you need to achieve unique values-only, you should use Set. You have to use your own equals & hashCode implementation, for example, for case 1):

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Details details = (Details) o;
        return Objects.equals(name, details.name) &&
                Objects.equals(email, details.email);
    }
    
    @Override
    public int hashCode() {
    
        return Objects.hash(name, email);
    }
    

    If you need all Details members to be unique just update hash & equals implementation with needed properties.

提交回复
热议问题