1

在我的代码中,我有一组 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;
}
4

3 回答 3

2

试试龙目岛。我已将 GAgarwal 解决方案简化为微不足道的类。

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;
        }
}

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

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.14.8</version>
    <scope>provided</scope>
</dependency>
于 2015-05-05T12:37:33.057 回答
1

尝试这个

@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;
}
于 2015-03-02T09:17:55.473 回答
0
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);
        }
}
于 2015-03-02T07:42:03.037 回答