Sorted list of contacts having duplicates ,why?

前端 未结 7 1179
青春惊慌失措
青春惊慌失措 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 02:51

    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 form HashSet

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

提交回复
热议问题