如何从java

时间:2015-09-27 08:08:16

标签: java android arraylist hashset duplicate-removal

我的项目中ArrayList<CustomObject>ArrayList<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中重复的名称contains。我尝试了几种方法,如删除重复项,如下所示:

方法I:使用HashSet

将值添加到HashSet并将这些值分配回名为new ArrayList<Names>的{​​{1}}。

al

方法I的输出:

          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]

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

3 个答案:

答案 0 :(得分:2)

你只需要覆盖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);

    }
}

希望这有帮助!

答案 1 :(得分:2)

您需要覆盖equals类的hashcodeNames方法。

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

答案 2 :(得分:0)

为什么不使用Set for everything,而不是使用ArrayList。为了使Set正常工作,您将不得不覆盖 因此,Names.class中的equals(E对象)和hashcode()方法。有关更多提示,请参阅Java Generics:

Java Overriding equals and hashCode with Generics