简体   繁体   English

如何从ArrayList中删除重复值<CustomObject>在 Java 中

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

I have a ArrayList<CustomObject> as ArrayList<Names> in my project.我的项目中有一个ArrayList<CustomObject>作为ArrayList<Names> The Names pojo contains name and image fields as follows: Names pojo 包含名称和图像字段,如下所示:

Names.java名称.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;
    }

}

I am adding values for the fields as follows:我正在为字段添加值,如下所示:

         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"));

My requirement:我的要求:

Now all my requirement is to remove the repeated names contains in the ArrayList.现在我所有的要求是删除 ArrayList 中包含的重复名称。 I tried several methods like to remove duplicates as follows:我尝试了几种方法,例如删除重复项,如下所示:

Method I: Using HashSet方法一:使用HashSet

Adding values into the HashSet and assigning back those values into new ArrayList<Names> named al .将值添加到 HashSet 并将这些值分配回名为al new 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);

Output of Method I:方法一的输出:

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

Expected Output to be:预期输出为:

Values after removing duplicates:删除重复项后的值:

[karthick, vikki,chandru, vivek, harish]

I am also posting my entire class for your reference我也把我的整个班级都贴出来供大家参考

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);

     }
 }

Please help me to resolve by issue which I am facing to remove duplicates from the list.请帮助我解决从列表中删除重复项所面临的问题。 Any kind of suggestions and solutions would be much helpful for me.任何类型的建议和解决方案都会对我有很大帮助。 Thanks in advance.提前致谢。

You just need to override hashCode and equals and your code works well:您只需要覆盖 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;
    }


}

Test 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);

    }
}

Hope this help!希望这有帮助!

You need to override equals and hashcode method for Names class.您需要覆盖Names类的equalshashcode方法。

The hashcode method is use to calculate the bucket in HashSet and equals is used to identify the duplicate and will not add if any found already in the set. hashcode方法用于计算HashSet的bucket, 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;
}

Why don't use use a Set for everything, instead of using the ArrayList.为什么不对所有内容都使用 Set,而是使用 ArrayList。 In order for the Set to work properly you will have to overwrite the the equals(E object), and hashcode() methods in your Names.class accordingly.为了使 Set 正常工作,您必须相应地覆盖 Names.class 中的 equals(E object) 和 hashcode() 方法。 Please see Java Generics for more hints:有关更多提示,请参阅 Java 泛型:

Java Overriding equals and hashCode with Generics Java用泛型覆盖equals和hashCode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM