Friday, 15 June 2012

HashMap Duplicate Key Replacement java -



HashMap Duplicate Key Replacement java -

i have thing , lets hashmap> has <> "11,name2,name1" , "14,name4,name5"...so i'm trying remove 11 first , info name2 , name1 transferred 14. problem 14's own original info name4 , name 5 overwritten. want maintain them in 14. how should that? hope can help me it. give thanks you.

list<string> oldvalue = map.remove(oldkey); map.put(newkey, oldvalue);

you can seek next way. extract old list old key. , add together list items list of new key.

// remove old key list<string> oldvalue = map.remove(oldkey); // add together new key empty list if not exists if(!map.containskey(newkey)) { map.put(newkey, new linkedlist<string>()); } // new list new value list<string> newvalue = map.get(newkey); // add together items old value new list for(string s : oldvalue){ // eliminate possible duplicates if(!newvalue.contains(s)){ newvalue.add(s); } }

java hashmap

No comments:

Post a Comment