anu_r
anu_r

Reputation: 1622

how to remove duplicate values in hashmap

i have a hashmap and i am entering values in the hashmap using the same keys.I want to inser the value in the database only if that value is not present in the databse.

here's my code

 s11=json_data.getString("phno");
        Iterator myVeryOwnIterator = map.keySet().iterator();
        while(myVeryOwnIterator.hasNext()) {                                              
    String key=(String)myVeryOwnIterator.next();
    String value=(String)map.get(key);
         if(s11.compareTo(value)==0)
         {//String name2 = cursor.getString(cursor.getColumnIndex(s11 ));
         queryValues.put("c",value);
         //Cursor cursr = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?",           new String[] { s11 }, null);
         // Toast.makeText(getActivity(),"get particular name", Toast.LENGTH_LONG).show();
         db.insertcontact(queryValues);
         // Toast.makeText(getActivity(),"true", Toast.LENGTH_LONG).show();
         }
         }

Upvotes: 1

Views: 2207

Answers (1)

Bhavesh Vadalia
Bhavesh Vadalia

Reputation: 814

can you use this way?

HashMap<String,Integer> hm=new HashMap<String,Integer>(); 
hm.put("AK",1); 
hm.put("BK",2); 
hm.put("CK",3); 
hm.put("DK",4); 
hm.put("EK",5); 
hm.put("FK",6); 
hm.put("GK",7); 
hm.put("HK",6); 
hm.put("IK",7); 


System.out.println("values are "+hm.values()); 

// Removing the duplicate VALUES from Map 
System.out.println("\n After removing duplicate values "); 

for(Object key1:hm.keySet()){ 

for(Object key2:hm.keySet()){ 
if(!key1.toString().equals(key2.toString())){ 
int x=hm.get(key1); 
int y=hm.get(key2); 
if(x==y){ 
    hm.remove(key2); 
} 
} 

}
}

Upvotes: 4

Related Questions