简体   繁体   中英

logic for removing duplicates from hashset

Here is the scenario. I want to avoid duplicates only if 2 among the 3 fields are same value. Id will be different but if name and address both are same then that should be avoided.

I tried the following code where I add some name, id and address

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

I have one Employee class with setters and getters and constructor of my choice.

I wanna avoid printing if name and address (both) are gonna be repeated.

1 A xxx
2 A xxx The above scenario should be avoided

Can you please help me with logic ?

In your Employee class, implement equals() and hashCode() according to your rules:

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

Implementing equals() and hashCode() is certainly something you should consider. If (for whatever reason) you only have id in your equals()/hashCode() or the other attributes are not that appropriate to check there, you may also want to consider filtering out those duplicates "manually".

This question has already good answers to solve that problem.

You can also use non default equals and hashCode . If you want, you can use eg guava or apache.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM