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;

    }

}
1

2 Answers 2

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

}
Sign up to request clarification or add additional context in comments.

3 Comments

will it change the order of rows ?
if you want to preserve order, you can use LinkedHashSet instead
Thanks last thing what is the equivalent LinkedHashSet of arReceipient.get(x).sId
0
ArrayList list = new ArrayList();

HashSet hashmap = new HashSet();
hashmap.addAll(list);
list.clear();
list.addAll(hashmap);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.