Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从哈希集中删除重复项的逻辑_Java_Collections_Hashset - Fatal编程技术网

Java 从哈希集中删除重复项的逻辑

Java 从哈希集中删除重复项的逻辑,java,collections,hashset,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) {

下面是一个场景。我只想在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());  
}  
HashSet mySet=newhashset();
mySet.add(新员工(1,“a”,“xxx”);
mySet.add(新员工(2,“a”,“yyy”);
对于(员工emp:mySet){
System.out.println(emp.getId()+“”+emp.getName()+“”+emp.getAddress());
}  
我有一个Employee类,其中包含我选择的setters和getter以及构造函数

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

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


你能帮我讲一下逻辑吗

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);
    }
}
< >代码> >均衡器()/代码>和<代码> HASCODE()当然是你应该考虑的。 如果(无论出于什么原因),您在<代码>均衡器()/HASCODE()/代码>中只有<代码> ID <代码>,或者其他属性不适合检查它,您可能还想考虑过滤掉那些“手动”重复。


已经有很好的答案来解决这个问题。

您还可以使用非默认的
equals
hashCode
。如果您愿意,您可以使用番石榴或apache

番石榴

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

非常感谢。这帮了大忙