簡體   English   中英

用於從hashset中刪除重復項的邏輯

[英]logic for removing duplicates from 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個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);
    }
}

實現equals()hashCode()肯定是你應該考慮的事情。 如果(無論出於什么原因)你只在equals()/hashCode()id ,或者其他屬性不適合檢查那里,你可能還想考慮“手動”過濾掉那些重復項。

這個問題已經很好地解決了這個問題。

您還可以使用非默認的equalshashCode 如果你願意,你可以使用例如番石榴或阿帕奇。

番石榴

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM