2015-12-15 10 views
5

मैं सामग्री प्रदाता से संपर्क लाना हूँ की ArrayList से डुप्लिकेट तत्वों को दूर नहीं किया जा सकता और मैं इस प्रक्रिया मैं सफलतापूर्वक संपर्कों को लाने के लिए एक listview.In में उन्हें प्रदर्शित करने की आवश्यकता है, लेकिन वे डुप्लिकेट मानों अब Phone.CONTACT_ID शामिल प्रत्येक संपर्क के लिए अद्वितीय है। मैं उस विशेष फ़ील्ड के आधार पर संपर्कों की अपनी सरणी सूची फ़िल्टर करना चाहता हूं।hashmap

यहाँ कोड है:

try { 
      cursor = getApplicationContext().getContentResolver() 
        .query(Phone.CONTENT_URI, null, null, null, null); 
      int Idx = cursor.getColumnIndex(Phone.CONTACT_ID); 
      int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME); 

      int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER); 
      int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_THUMBNAIL_URI); 
      cursor.moveToFirst(); 
      do { 
       System.out.println("=====>in while"); 
       HashMap<String, String> hashMap = new HashMap<String, String>(); 

       contactid=cursor.getString(Idx); 
       name = cursor.getString(nameIdx); 
       phoneNumber = cursor.getString(phoneNumberIdx); 
       image = cursor.getString(photoIdIdx); 
       System.out.println("Id--->"+contactid+"Name--->"+name); 

       if (!phoneNumber.contains("*")) { 
        hashMap.put("contactid", "" + contactid); 
        hashMap.put("name", "" + name); 
        hashMap.put("phoneNumber", "" + phoneNumber); 
        hashMap.put("image", "" + image); 
        // hashMap.put("email", ""+email); 
           hashMapsArrayList.add(hashMap); 
       } 

      } while (cursor.moveToNext()); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      if (cursor != null) { 
       cursor.close(); 
      } 
     } 

यहाँ आप देख सकते हैं कि मैं कर्सर से 4 क्षेत्रों प्राप्त करने में कठिनाई रहा हूँ और उन्हें ArrayList hashMapsArrayList जो ArrayList<HashMap<String, String>> hashMapsArrayList=new ArrayList<HashMap<String,String>>(); अब इस ArrayList डुप्लिकेट फ़ील्ड हैं है में बंधन और मैं चाहता हूँ इसे पर आधारित पर फ़िल्टर करें।

यहाँ कोड है:

System.out.println("Original--->"+hashMapsArrayList); 
    for(int i=0;i<hashMapsArrayList.size();i++) 
    { 
     HashMap<String, String> resultp = new HashMap<String, String>(); 
     resultp = hashMapsArrayList.get(i); 
     String contactiddup=resultp.get("contactid"); 


     if(!(hashMapsArrayListRemovedup.contains(contactiddup))) 
     { 
      System.out.println(hashMapsArrayListRemovedup); 
      System.out.println("In if added"); 
      hashMapsArrayListRemovedup.add(resultp); 
     }else{ 
      System.out.println("In else"); 
     } 

    } 

लेकिन इसके बाद के संस्करण कोड काम नहीं कर रहा है और नई ArrayList भी डुप्लिकेट मान हैं।

कृपया मदद करें।

एक नमूना उत्पादन duplicacy:

12-15 14:10:57.217: I/System.out(8971): [{name=Didi America, image=null, phoneNumber=, contactid=7996}, {name=Didi America, image=null, phoneNumber=, contactid=7996}] 
+0

क्रम में LinkedHashSet को ArrayList तत्वों जोड़े है – Sree

+0

http://stackoverflow.com/questions/203984/how-do-i-remove-repeated-elements-from-arraylist – Sunny

+0

आपके डुप्लिकेट मान का क्या अर्थ है? प्रदाता – pskink

उत्तर

3

आप कर सकते हैं, और जब आप सूची का निर्माण, जबकि पाश में डुप्लिकेट निकालने चाहिए। बस अद्वितीय आईडी एकत्र करने के लिए एक HashSet उपयोग करें, और केवल सूची के लिए नया एक पहचानकर्ता होने प्रविष्टियों जोड़ें:

 Set<String> ids = new HashSet<>(); 
     do { 
      System.out.println("=====>in while"); 
      contactid=cursor.getString(Idx); 
      if (!ids.contains(contactid)) { 
       ids.add(contactid); 
       HashMap<String, String> hashMap = new HashMap<String, String>(); 
       name = cursor.getString(nameIdx); 
       phoneNumber = cursor.getString(phoneNumberIdx); 
       image = cursor.getString(photoIdIdx); 
       System.out.println("Id--->"+contactid+"Name--->"+name); 

       if (!phoneNumber.contains("*")) { 
       hashMap.put("contactid", "" + contactid); 
       hashMap.put("name", "" + name); 
       hashMap.put("phoneNumber", "" + phoneNumber); 
       hashMap.put("image", "" + image); 
       // hashMap.put("email", ""+email); 
       hashMapsArrayList.add(hashMap); 
       } 
      } 
     } while (cursor.moveToNext()); 

आप दूसरी कोड का टुकड़ा (की जरूरत नहीं है कि मैं क्या उस में गलत क्या है देखने के लिए जाँच नहीं की थी)।

+0

मुझे कोशिश करें ...... – kgandroid

+0

उपरोक्त कोड का तर्क उत्कृष्ट है और यह काम करता है – kgandroid

+0

संक्षेप में आप कृपया एक नज़र डालें दूसरा कोड स्निपेट और मुझे बताओ कि मैं क्या गलत कर रहा था ?? – kgandroid

0

वैकल्पिक रूप से आप आवृत्ति के रूप में भी हटा सकते हैं। इस कोड को देखें। यह आपकी मदद कर सकता है।

Collections.frequency(arrayList, number) 

public static void main(String[] args) { 
    ArrayList<Integer> arrayList = new ArrayList<Integer>(); 
    arrayList.add(5); 
    arrayList.add(1); 
    arrayList.add(5); 
    arrayList.add(1); 
    arrayList.add(5); 
    arrayList.add(2); 
    arrayList.add(3); 

    ArrayList<Integer> unique = new ArrayList<>(); 

    for (Integer number : arrayList) { 
     if (Collections.frequency(arrayList, number) == 1) { 
      unique.add(number); 
     } 
    } 

    System.out.println(unique); 
} 
+0

यह – kgandroid

1

डुप्लिकेट प्रविष्टियों को हटाने के लिए HashSet का उपयोग करें।

उदा। HasSet को

public static HashSet<String> hSet= new HashSet<String>(); 

जोड़ें मूल्य HashSet से hSet.add(yourString);

का उपयोग कर प्राप्त मूल्य के लिए आप का उपयोग करने के डुप्लिकेट तत्वों को दूर करने के लिए Iterator

Iterator iterator = hSet.iterator(); 
while(iterator.hasNext()){ 
     String str = iterator.next(); 
} 
+0

उद्देश्य को हल नहीं करेगा मैंने कोशिश की हैशॉट .. लेकिन यह काम नहीं किया .. – kgandroid

संबंधित मुद्दे