如何重写 hashcode 和 equals 方法以避免在 java 中的 HashSet 中添加重复的字符串?

How to override hashcode and equals method to avoid adding duplicate strings in HashSet in java?

在我的代码中,我有一组 PlacesInfo 对象,即

 Set<PlacesInfo> placeId;    

在此集合中,我添加了 placeId(字符串)。我需要避免向我的 HashSet 添加重复项。下面是我的覆盖方法。但是,它仍然在我的集合中添加重复元素。那么,如何避免这种情况呢?

@Override
public int hashCode() {
    int hash = 5;
    hash = 97 * hash + Objects.hashCode(this.placeId);
    return hash;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) {
        return true;
    }
    if (this.getClass() != obj.getClass()) {
        return false;
    }
    final PlacesInfo other = (PlacesInfo) obj;
    if (!Objects.equals(this.placeId, other.placeId)) {
        return false;
    }
    return true;
}
Below code working fine.If you remove equals and hashcode then it will add two elements.

import java.util.HashSet;  
import java.util.Objects;  
import java.util.Set;

class PlacesInfo {  

        int placeId;

        public int getId() {
            return placeId;
        }

        PlacesInfo(int placeId) {
            this.placeId = placeId;
        }

        public void setId(int placeId) {
            this.placeId = placeId;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null)
                return true;
            if (this.getClass() != obj.getClass())
                return false;
            final PlacesInfo other = (PlacesInfo) obj;
            if (!Objects.equals(this.placeId, other.placeId))
                return false;
            return true;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 97 * hash + Objects.hashCode(this.placeId);
            return hash;
        }
   }

public class Test {

        public static void main(String[] args) {
            PlacesInfo t1 = new PlacesInfo(1);
            PlacesInfo t2 = new PlacesInfo(1);
            System.out.println(t1.equals(t2));
            Set<PlacesInfo> tempList = new HashSet<PlacesInfo>(2);
            tempList.add(t1);
            tempList.add(t2);
            System.out.println(tempList);
        }
}

试试这个

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((placeId == null) ? 0 : placeId.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    PlacesInfo other = (PlacesInfo) obj;
    if (placeId == null) {
        if (other.placeId != null)
            return false;
    } else if (!placeId.equals(other.placeId))
        return false;
    return true;
}

尝试Lombok。我已将 GAgarwal 解决方案简化为一个微不足道的 class.

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

@EqualsAndHashCode(of={"placeId"})
public class PlacesInfo{

        @Getter; @Setter;
        int placeId;

        PlacesInfo(int placeId) {
            this.placeId = placeId;
        }
}

Lombok 由 Maven 提供。您不需要将它包含在最终的 jar 中。仅用于编译。

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.14.8</version>
    <scope>provided</scope>
</dependency>