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
You can try with HashSet.
public class HashSet extends AbstractSet implements Set, Cloneable, Serializable
Code Structure
HashSet hashSET = new HashSet();
hashSET.add("AA");
hashSET.add("BB");
hashSET.add("CC");
hashSET.add("AA"); // Adding duplicate elements
Then
Iterator j = hashSET.iterator();
while (j.hasNext())
System.out.println(j.next()); // Will print "AA" once.
}
Now SORT your Hashset Values using TreeSet.
TreeSet implements the SortedSet interface so duplicate values are not allowed.
TreeSet _treeSET= new TreeSet(hashSET);