集不包含等于其成员之一的项目?

时间:2018-12-19 20:50:34

标签: java set equals contains

因此,根据the Java APISet::contains

  

当且仅当此集合包含元素e使得(o == null?e == null:o.equals(e))时返回true

那么...为什么即使该集合中恰好包含一个等于creds的元素,该方法也会返回false?

private boolean validate(Credentials creds){
    Set<Credentials> acceptedUsers = getAcceptedUsers();
    return acceptedUsers.contains(creds);
}

更具体地说,我插入了一些printlns

private boolean validate(Credentials creds){
    Set<Credentials> acceptedUsers = getAcceptedUsers();

    System.out.print("accepted users: ");
    System.out.println(acceptedUsers);
    System.out.print("accessing user: ");
    System.out.println(creds);
    System.out.println("items are equal: " + acceptedUsers.stream().map(c -> c.equals(creds)).collect(Collectors.toSet()));
    System.out.println("access ok: " + (acceptedUsers.contains(creds) ? "YES" : "NO"));

    return acceptedUsers.contains(creds);
}

这就是它的意思:

accepted users: [[
    foo
    FCDE2B2EDBA56BF408601FB721FE9B5C338D10EE429EA04FAE5511B68FBF8FB9
]]
accessing user: [
    foo
    FCDE2B2EDBA56BF408601FB721FE9B5C338D10EE429EA04FAE5511B68FBF8FB9
]
items are equal: [true]
access ok: NO

getAcceptedUsers当前返回一个虚拟集

private Set<Credentials> getAcceptedUsers(){
    return new HashSet<Credentials>(){{add(new Credentials("foo","bar", false));}};
}

Credentials的实现方式为

class Credentials{
    final String username;
    final String password;

    public Credentials(String username, String password, boolean isPasswordHashed) {
        this.username = username;

        if(isPasswordHashed) this.password = password;
        else {
            MessageDigest md;
            try {
                md = MessageDigest.getInstance("SHA-256");
            } catch (NoSuchAlgorithmException e) {
                throw new IllegalStateException(e);
            }

            md.update(password.getBytes());
            byte[] hash = md.digest();

            this.password = (new HexBinaryAdapter()).marshal(hash);
        }
    }

    @Override
    public boolean equals(Object obj) {
        if(obj == null) return false;
        if(!(obj instanceof Credentials)) return false;
        Credentials other = (Credentials)obj;
        return this.username.equals(other.username) && this.password.equals(other.password);
    }

    @Override
    public String toString() {
        return String.format("[\n\t%s\n\t%s\n]", username,password);
    }
}

2 个答案:

答案 0 :(得分:5)

仅使用equals方法是不够的,还必须实现hashCode

就像您可以阅读here

  

返回此集合的哈希码值。集合的哈希码定义为集合中元素的哈希码之和,其中空元素的哈希码定义为零。这样可以确保s1.equals(s2)隐含了Object.hashCode()的一般合同要求的任意两个s1和s2集的s1.hashCode()== s2.hashCode()。

答案 1 :(得分:3)

如果要使用HashMapHashSet,则需要覆盖hashCode()方法,以便相等的对象具有相同的哈希码。例如:

@Override
public int hashCode() {
    return Objects.hash(username, password);
}