I have contacts that are being added to an arraylist, now some contacts are common in other applications so duplicate entries are also saved in that.
I’m aware of two approaches to resolve this situation, I’m not sure which approach should I follow ?
first:
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
ArrayList<String> temp = new ArrayList<String>();
temp.add(name);
if (!contactList.contains(name)) {
contactList.add(name);}
second:
Adding it to hashset as it doesn’t allow duplicate entries and then adding it back to arraylist-
String name = getStrin(...);
contactList.add(name);
// after the loop has completed adding all elements
HashSet hs = new HashSet();
hs.addAll(contactList);
contactList.clear();
contactList.addAll(hs);
You should follow the second way, as it is better, and optimized for distinct values.