Sorted list of contacts having duplicates ,why?

前端 未结 7 1191
青春惊慌失措
青春惊慌失措 2020-12-07 02:26

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

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 03:02

    You can try with HashSet.

    public class HashSet extends AbstractSet implements Set, Cloneable, Serializable

    • Duplicate values are not allowed.

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

提交回复
热议问题