Unable to remove duplicate items from arraylist hashmap

I get contacts from a content provider and I need to display them in a list. In the process, I successfully retrieve the contacts, but they contain duplicate values .Now Phone.CONTACT_ID is unique for each contact. I want to filter my arraylist of contacts based on this specific field.

Here is the code:

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

Here you can see that I am extracting 4 fields from the cursor and bind them to arraylist hashMapsArrayList , which is equal to ArrayList<HashMap<String, String>> hashMapsArrayList=new ArrayList<HashMap<String,String>>(); . Now this arraylist contains duplicate fields, and I want to filter it based on contactid .

Here is the code:

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

But the code above does not work, and the new arraylist also contains duplicate values.

Please, help.

Example duplication of output:

 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}] 
+5
source share
3 answers

You can and should remove duplicates when building a list in a while loop. Just use a HashSet to collect unique identifiers and add only entries with a new identifier to the list:

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

You do not need a second piece of code (I did not check to see what is wrong with it).

+3
source

Use a HashSet to delete duplicate entries.

Eg.

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

Add a value to HasSet using hSet.add(yourString);

To get value from HashSet you need to use Iterator

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

Alternatively, you can also check the frequency of deletion. refer to this code. This can help you.

 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
source

Source: https://habr.com/ru/post/1238235/


All Articles