[英]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.
我想避免重复,只要3个字段中的2个是相同的值。 Id will be different but if name and address both are same then that should be avoided.
ID会有所不同,但如果名称和地址都相同,则应避免使用。
I tried the following code where I add some name, id and address 我尝试了以下代码,我添加了一些名称,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());
}
I have one Employee class with setters and getters and constructor of my choice. 我有一个带有setter和getter的Employee类以及我选择的构造函数。
I wanna avoid printing if name and address (both) are gonna be repeated. 如果姓名和地址(两者)都要重复,我想避免打印。
1 A xxx 1个xxx
2 A xxx The above scenario should be avoided 2 A xxx应避免上述情况
Can you please help me with logic ? 你能用逻辑来帮助我吗?
In your Employee
class, implement equals()
and hashCode()
according to your rules: 在您的
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);
}
}
Implementing equals()
and hashCode()
is certainly something you should consider. 实现
equals()
和hashCode()
肯定是你应该考虑的事情。 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". 如果(无论出于什么原因)你只在
equals()/hashCode()
有id
,或者其他属性不适合检查那里,你可能还想考虑“手动”过滤掉那些重复项。
This question has already good answers to solve that problem. 这个问题已经很好地解决了这个问题。
You can also use non default equals
and hashCode
. 您还可以使用非默认的
equals
和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 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.