用于从hashset

时间:2017-01-20 08:24:20

标签: java collections hashset

以下是该方案。我想避免重复,只要3个字段中的2个是相同的值。 ID会有所不同,但如果名称和地址都相同,则应避免使用。

我尝试了以下代码,其中添加了一些名称,ID和地址

HashSet<Employee> mySet = new HashSet<Employee>();
mySet.add(new Employee (1,"a","xxx"));
mySet.add(new Employee(2,"a", "yyy"));

for(Employee emp : mySet) {  
    System.out.println(emp.getId() + " " + emp.getName()+" "+emp.getAddress());  
}  

我有一个带有setter和getter以及我选择的构造函数的Employee类。

如果要重复姓名和地址(两者),我想避免打印。

1 A xxx
2 A xxx 应避免上述情况

你能帮我逻辑一下吗?

3 个答案:

答案 0 :(得分:3)

Employee课程中,根据您的规则实施equals()hashCode()

class Employee {
    private int id;
    private String name;
    private String address;

    public Employee(int id, String name, String address) {
        this.id = id;
        this.name = name;
        this.address = address;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return Objects.equals(name, employee.name) &&
                Objects.equals(address, employee.address);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, address);
    }
}

答案 1 :(得分:0)

实施equals()hashCode()肯定是您应该考虑的事情。 如果(无论出于何种原因)您的id中只有equals()/hashCode()或其他属性不适合检查,那么您可能还需要考虑过滤掉那些重复的内容&#34;手动&#34 ;

This question已经很好地解决了这个问题。

答案 2 :(得分:0)

您还可以使用非默认equalshashCode。如果你愿意,你可以使用例如番石榴或阿帕奇。

<强> GUAVA

import com.google.common.base.Objects;

class Employee {
  private int id;
  private String name;
  private String address;

  public Employee(int id, String name, String address) {
    this.id = id;
    this.name = name;
    this.address = address;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Employee employee = (Employee) o;
    return id == employee.id &&
            Objects.equal(name, employee.name) &&
            Objects.equal(address, employee.address);
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(id, name, address);
  }
}

<强> APACHE

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

class Employee {
  private int id;
  private String name;
  private String address;

  public Employee(int id, String name, String address) {
    this.id = id;
    this.name = name;
    this.address = address;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;

    if (o == null || getClass() != o.getClass()) return false;

    Employee employee = (Employee) o;

    return new EqualsBuilder()
            .append(id, employee.id)
            .append(name, employee.name)
            .append(address, employee.address)
            .isEquals();
  }

  @Override
  public int hashCode() {
    return new HashCodeBuilder(17, 37)
            .append(id)
            .append(name)
            .append(address)
            .toHashCode();
  }
}