java 如何从java arraylist中删除重复的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30745048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:35:20  来源:igfitidea点击:

how to remove duplicate objects from java arraylist

javaobjectarraylist

提问by Peter Pan

I have requirement to remove the duplicate object from my arraylist of object. I tried it by making to arraylist of objects. First one contains all objects including duplicate and the another contain only the unique one.

我需要从我的对象数组列表中删除重复的对象。我通过制作对象的数组列表来尝试它。第一个包含所有对象,包括重复项,另一个只包含唯一的对象。

ArrayList<ListTableClass> ltc = new ArrayList<ListTableClass>();//has duplicate
ArrayList<ListTableClass> ltc2 = new ArrayList<ListTableClass>();//unique

And i used contains method to check for dublicates like this:

我使用 contains 方法来检查这样的重复项:

for (ListTableClass element : ltc) {
          if (!ltc2.contains(element)) {
            ltc2.add(element);
          }
    }

but this does not remove duplicates. It adds all the elements of ltc to ltc2. Don't know why? ltc does contain duplicate objects.

但这不会删除重复项。它将 ltc 的所有元素添加到 ltc2。不知道为什么?ltc 确实包含重复的对象。

回答by Peter Pan

Because the function "contains" of ArrayList compare with two objects in their functions hashcode & equals, so you must override the function "hashCode" & "equals" of Class ListTableClass.

因为ArrayList的函数“contains”与两个对象的函数hashcode&equals比较,所以必须重写ClassListTableClass的函数“hashCode”&“equals”。

example:

例子:

import java.util.ArrayList;
public class ListTableClass {
    private String name;
    private int age;

public ListTableClass(String name, int age) {
    super();
    this.name = name;
    this.age = age;
}

public static void main(String[] args) {
    ArrayList<ListTableClass> ltc = new ArrayList<ListTableClass>();// has duplicate
    ListTableClass obj0 = new ListTableClass("A", 0);
    ListTableClass obj1 = new ListTableClass("B", 1);
    ListTableClass obj2 = new ListTableClass("C", 2);
    ListTableClass obj3 = new ListTableClass("A", 0);
    ltc.add(obj0);
    ltc.add(obj1);
    ltc.add(obj2);
    ltc.add(obj3);
    ArrayList<ListTableClass> ltc2 = new ArrayList<ListTableClass>();// unique
    for (ListTableClass element : ltc) {
        if (!ltc2.contains(element)) {
            System.out.println(element);
            ltc2.add(element);
        }
    }
}
}

output before override hashCode:

覆盖 hashCode 之前的输出:

y2015.m06.d10.ListTableClass@659e0bfd //obj0 {"A", 0}
y2015.m06.d10.ListTableClass@2a139a55 //obj1 {"B", 1}
y2015.m06.d10.ListTableClass@15db9742 //obj2 {"C", 2}
y2015.m06.d10.ListTableClass@6d06d69c //obj3 {"A", 0} is different from obj0

add override function hashCode:

添加覆盖函数 hashCode:

@Override
 public int hashCode() {
 final int prime = 31;
 int result = 1;
 result = prime * result + age;
 result = prime * result + ((name == null) ? 0 : name.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;
    ListTableClass other = (ListTableClass) obj;
    if (age != other.age)
        return false;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    return true;
}

then output is:

然后输出是:

y2015.m06.d10.ListTableClass@402 //obj0
y2015.m06.d10.ListTableClass@422 //obj1
y2015.m06.d10.ListTableClass@442 //obj2

there is not obj3 because of obj3's hashcode & its property is equal with obj0

没有 obj3 因为 obj3 的哈希码 & 它的属性等于 obj0