0

ArrayList arReceipient = new ArrayList(); is declared globally. The arraylist is populated as follows

arReceipient.add(new MyItem(data.get(i).getId(),data.get(i).getNickName()));

Resulting in

sId 1000002327 sName htc1
sId 1000002208 sName htcandroid
sId 1000002208 sName htcandroid
sId 1000002242 sName htcandroid1
sId 1000000721 sName bachan
sId 1000000721 sName bachan
sId 1000000810 sName bachan2

How can i remove duplicates entries such that result is

 sId 1000002327 sName htc1
 sId 1000002208 sName htcandroid
 sId 1000002242 sName htcandroid1
 sId 1000000721 sName bachan
 sId 1000000810 sName bachan2

Here is MyItem class

public class MyItem {

    public String sId;
    public String sName;

    public MyItem(String sid, String sname){

        this.sId=sid;
        this.sName=sname;

    }

}
Dimitri
  • 1,924
  • 9
  • 43
  • 65

2 Answers2

1

instead of list use set.

LinkedHashSet<MyItem> arReceipient =new LinkedHashSet<MyItem>();

and add equals method in MyItem

public class MyItem {

    public String sId;
    public String sName;

    public MyItem(String sid, String sname){

        this.sId=sid;
        this.sName=sname;

    }
   public boolean equals(Object o){
      if(!(o instanceof MyItem)) return false;
      return sId==((MyItem)o).sId;
  }

}
vipul mittal
  • 17,343
  • 3
  • 41
  • 44
0
ArrayList list = new ArrayList();

HashSet hashmap = new HashSet();
hashmap.addAll(list);
list.clear();
list.addAll(hashmap);
Robi Kumar Tomar
  • 3,418
  • 3
  • 20
  • 27