2014-02-10 15 views
1

刪除重複值有一個HashMap和我使用同樣的keys.I要inser在數據庫中的值,只有當該值不存在DATABSE在HashMap中輸入值。我如何在HashMap中

這裏是我的代碼

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

我建議重寫equals方法到您的類,並且 – meh

+0

之前添加值到HashMap中使用的方法包含(OBJ)之前,我會建議使用的HashSet您的需求的http://計算器。 com/questions/15579863 /使用hashset-over-hashmap的優點http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util /HashSet.java – Gattsu

回答

4

,你可以用這樣的方式?

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

} 
}