比较列表中的元素,如果某些属性相同则删除

时间:2013-01-30 11:51:46

标签: java collections traversal

我有一个对象的数组列表,其中包含例如:

  • 名称
  • 地址
  • 电话
  • 许多其他物业......

如果某些属性与数组列表中的其他对象具有相同的值,我希望删除此列表中的某些对象。我需要遍历整个列表,看看这个列表中是否已存在名称,地址和电话。我不能做一个简单的事情:

for (...)
if (!newlist.contains(element)) { newlist.add(element); }

在将element添加到新列表之前,我只需检查特定属性是否相同。

有人能引导我朝正确的方向前进吗?

2 个答案:

答案 0 :(得分:3)

如何在自定义比较器中使用Set?让你的对象类实现Comparable。在比较方法中,您可以编写测试以准确匹配对象。

答案 1 :(得分:1)

创建一个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类让我们假设一个包含main方法的类说 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]