I have sorted and listed my phone contacts in to an arraylist but ,i got many duplicates of same contact names in the list .How this happens? how to avoid this?
This
May be in your contacts having multiple groups, and that group will be a WhatsApp,Google etc..Go to your contacts and search that contact having whatsApp account. will showing double entry with different Group
you should use or change your ContactsBean
, in your Bean
use HashSet
Note:
HashSet
can avoid duplicate entry more
HashSet
contains unique elements only,it can avoid same key element formHashSet
Example bean
public class ContactBean {
private HashSet number = new HashSet();
public void setNumber(String number) {
if (number == null)
return;
this.number.add(number.trim());
}
public HashSet getNumber() {
return this.number;
}
}
Simple Example
//Creating HashSet and adding elements
HashSet hashSet=new HashSet();
hashSet.add("Dhruv");
hashSet.add("Akash");
hashSet.add("Dhruv"); //Avoiding this entry
hashSet.add("Nirmal");
//Traversing elements
Iterator itr = hashSet.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}