我有一个对象数组列表,其中包含例如:
- 姓名
- 地址
- 电话
- 许多其他属性...
如果某些属性与数组列表中的其他对象具有相同的值,我希望删除此列表中的某些对象。我需要遍历整个列表并查看名称、地址和电话是否已存在于此列表中。我不能做一个简单的:
for (...)
if (!newlist.contains(element)) { newlist.add(element); }
因为我只需要在添加element
到新列表之前检查特定属性是否相同。
谁能指导我正确的方向?
我有一个对象数组列表,其中包含例如:
如果某些属性与数组列表中的其他对象具有相同的值,我希望删除此列表中的某些对象。我需要遍历整个列表并查看名称、地址和电话是否已存在于此列表中。我不能做一个简单的:
for (...)
if (!newlist.contains(element)) { newlist.add(element); }
因为我只需要在添加element
到新列表之前检查特定属性是否相同。
谁能指导我正确的方向?
将 Set 与自定义 Comparator 一起使用怎么样?让您的对象类实现 Comparable。在 compare 方法中,您可以编写测试以完全匹配您需要的对象。
创建一个 Key Class,让我们用下面的代码说Employee.java 。
package com.innovation;
public class Employee {
private String name;
private String address;
private String phone;
public Employee() {
super();
}
public Employee(String name, String address, String phone) {
super();
this.name = name;
this.address = address;
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((phone == null) ? 0 : phone.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (phone == null) {
if (other.phone != null)
return false;
} else if (!phone.equals(other.phone))
return false;
return true;
}
@Override
public String toString() {
return "Employee [name=" + name + ", address=" + address + ", phone="
+ phone + "]";
}
}
现在创建一个要应用逻辑的客户端类,让我们假设一个包含主方法的类,比如Client.java
package com.innovation;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Client {
public static void main(String[] args) {
Set<Employee> empSet = new HashSet<Employee>(populateList());
for (Employee employee : empSet)
{
System.out.println(employee);
}
}
public static List<Employee> populateList()
{
List<Employee> lsts = new ArrayList<Employee>();
lsts.add(new Employee("rais","gurgaon","123456"));
lsts.add(new Employee("alam","Delhi","123685"));
lsts.add(new Employee("shyam","Mumbai","1257456"));
lsts.add(new Employee("ramesh","Ahmadabad","196356"));
lsts.add(new Employee("rais","gurgaon","123456"));
lsts.add(new Employee("rais","gurgaon","123456"));
lsts.add(new Employee("rais","gurgaon","123456"));
return lsts;
}
}
你会看到下面的输出。可以清楚地看到列表中存在的重复条目在集合中被删除。这一切都是很好地实现equals和hashcode方法的魔力。
Employee [name=rais, address=gurgaon, phone=123456]
Employee [name=ramesh, address=Ahmadabad, phone=196356]
Employee [name=alam, address=Delhi, phone=123685]
Employee [name=shyam, address=Mumbai, phone=1257456]