如何从 java 中的 ArrayList<CustomObject> 中删除重复值

How to remove duplicates value from ArrayList<CustomObject> in java

我的项目中有一个 ArrayList<CustomObject> 作为 ArrayList<Names>。 Names pojo 包含名称和图像字段,如下所示:

Names.java

public class Names {


    private String name;

    private String image;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public Names(String name, String image) {
        this.name = name;
        this.image = image;
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return name;
    }

}

我正在为以下字段添加值:

         ArrayList<Names> menu = new ArrayList<Names>();

         menu.add(new Names("chandru","image1"));
         menu.add(new Names("vikki","image2"));
         menu.add(new Names("karthick","image3"));
         menu.add(new Names("chandru","image4"));
         menu.add(new Names("karthick","image5"));
         menu.add(new Names("chandru","image6"));
         menu.add(new Names("karthick","image7"));
         menu.add(new Names("vikki","image8"));
         menu.add(new Names("karthick","image9"));
         menu.add(new Names("harish","image10"));
         menu.add(new Names("vivek","image11"));
         menu.add(new Names("harish","image12"));

我的要求:

现在我的所有要求是删除 ArrayList 中包含的重复名称。我尝试了几种方法,例如删除重复项,如下所示:

方法一: 使用 HashSet

将值添加到 HashSet 并将这些值分配回名为 alnew ArrayList<Names>

          ArrayList<Names> al = new ArrayList<Names>();

          Set<Names> hs = new HashSet<Names>();
          hs.addAll(menu);

          al.clear();
          al.addAll(hs);

          System.out.println(al);

方法一的输出:

[karthick, vikki, karthick, karthick, chandru, vivek, vikki, chandru, harish, harish, karthick, chandru]

预期输出为:

删除重复项后的值:

[karthick, vikki,chandru, vivek, harish]

我也把我的全部class贴出来供大家参考

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;


public class Sample {

     public static void main(String[] args) {

         ArrayList<Names> menu = new ArrayList<Names>();
         menu.add(new Names("chandru","image"));
         menu.add(new Names("vikki","image"));
         menu.add(new Names("karthick","image"));
         menu.add(new Names("chandru","image"));
         menu.add(new Names("karthick","image"));
         menu.add(new Names("chandru","image"));
         menu.add(new Names("karthick","image"));
         menu.add(new Names("vikki","image"));
         menu.add(new Names("karthick","image"));
         menu.add(new Names("harish","image"));
         menu.add(new Names("vivek","image"));
         menu.add(new Names("harish","image"));

         ArrayList<Names> al = new ArrayList<Names>();

          Set<Names> hs = new HashSet<Names>();
          hs.addAll(menu);

          al.clear();
          al.addAll(hs);

          System.out.println(al);

     }
 }

请帮我解决我面临的问题,从列表中删除重复项。任何类型的建议和解决方案都会对我有很大帮助。提前致谢。

您只需覆盖 hashCode 和 equals,您的代码就可以正常工作:

   public class Names {


    private String name;

    private String image;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public Names(String name, String image) {
        this.name = name;
        this.image = image;
    }

    @Override
    public String toString() {
        return name;
    }

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


}

测试Class

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class Test {


    public static void main(String[] args) {

        ArrayList<Names> menu = new ArrayList<Names>();
        menu.add(new Names("chandru","image"));
        menu.add(new Names("vikki","image"));
        menu.add(new Names("karthick","image"));
        menu.add(new Names("chandru","image"));
        menu.add(new Names("karthick","image"));
        menu.add(new Names("chandru","image2"));
        menu.add(new Names("karthick","image"));
        menu.add(new Names("vikki","image"));
        menu.add(new Names("karthick","image"));
        menu.add(new Names("harish","image"));
        menu.add(new Names("vivek","image"));
        menu.add(new Names("harish","image"));

        ArrayList<Names> al = new ArrayList<Names>();

         Set<Names> hs = new HashSet<Names>();
         hs.addAll(menu);

         al.clear();
         al.addAll(hs);

         System.out.println(al);

    }
}

希望对您有所帮助!

您需要为 Names class 覆盖 equalshashcode 方法。

hashcode方法用于计算HashSet中的桶,equals用于识别重复项,如果已经在集合中找到则不会添加。

@Override
public boolean equals(Object o) {
  if (this == o) return true;
  if (!(o instanceof Names)) return false;

  Names names = (Names) o;

  if (image != null ? !image.equals(names.image) : names.image != null) return false;
  if (name != null ? !name.equals(names.name) : names.name != null) return false;

  return true;
}

@Override
public int hashCode() {
  int result = name != null ? name.hashCode() : 0;
  result = 31 * result + (image != null ? image.hashCode() : 0);
  return result;
}

为什么不对所有内容都使用 Set,而是使用 ArrayList。为了使 Set 正常工作,您必须覆盖 相应地,Names.class 中的 equals(E object) 和 hashcode() 方法。请参阅 Java 泛型以获得更多提示:

Java Overriding equals and hashCode with Generics