【问题标题】:How to remove duplicates value from ArrayList<CustomObject> in java如何从 java 中的 ArrayList<CustomObject> 中删除重复值
【发布时间】:2015-12-24 16:25:40
【问题描述】:

我的项目中有一个ArrayList&lt;CustomObject&gt; 作为ArrayList&lt;Names&gt;。 Names pojo 包含 name 和 image 字段如下:

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&lt;Names&gt;

          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]

我也发布了我的整个课程供你参考

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

     }
 }

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

【问题讨论】:

  • 您没有在 Names 类中覆盖 hashCode()equals()
  • 如果你想让HashSet根据Namesname消除重复,那么两个Names实例具有相同的name应该是相等的。但它们不是,因为您还没有覆盖 equals()hashCode() 来实现这一点。
  • @JBNizet 我真的不知道该怎么做。可以发一段代码吗??
  • @Chandru 阅读了方法的 javadoc 以了解他们的合同。使用谷歌。做一些研究。这是重要的东西。你不应该只拿我的代码然后复制粘贴。您需要了解它是如何工作的。

标签: java android arraylist hashset duplicate-removal


【解决方案1】:

你只需要重写 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;
    }


}

测试类

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

    }
}

希望对您有所帮助!

【讨论】:

  • 现在将第二个chandru的图像设置为“image2”。它仍然做 OP 想要的吗?
  • 是的,如果我将第二个图像字段更改为 image2,上述代码将不起作用。
  • @Nizet:取决于您要比较的内容。如果您只需要检查名称是否重复,只需删除equals和hashCode中的图像条件检查即可。
  • @Chandru:正如我在上面分享的,取决于您的要求。如果您只需要检查名称,则只需删除图像。让我为你编辑代码!
  • 是的@KennyTaiHuynh。无论如何,ArrayList 中的每个图像字段都会有所不同。我也会编辑问题。
【解决方案2】:

您需要为Names 类覆盖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;
}

【讨论】:

    【解决方案3】:

    为什么不使用 Set 来做所有事情,而不是使用 ArrayList。为了使 Set 正常工作,您必须覆盖 相应的 Names.class 中的 equals(E object) 和 hashcode() 方法。有关更多提示,请参阅 Java 泛型:

    Java Overriding equals and hashCode with Generics

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-08
      • 2010-12-25
      • 2014-03-25
      • 2015-08-25
      • 2020-03-06
      • 1970-01-01
      • 1970-01-01
      • 2019-02-23
      相关资源
      最近更新 更多