我有一个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 :(得分: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);
}
}
}
}