Hey all, I wasn't finding the exact same situation in my searches some forgive me if been asked before.
I got a situation where I have some string values code iterates through vales and builds a hashmap of keys and lists pertaining to the keys.
Well I recent discovered when the values in a list are duplicates of a prior list my key value changes to the last one it found.
For example hashmap has keys "1","2" each key has a list of strings.
Well when key "3" comes in and it has some of the same values in the list as key "1" , my hashmap now changes to "3" and "2" and key one with all "1" values disappear with key "3"'s values. IS there a map type to use or some way to prevent this?
I dunno... a Map<String, List<String>> is what you described, and it sounds like that's the way you wrote your code. But perhaps you've only got one List<String> object and you're using that for all of your map values? That sort of sounds like what you described. However, as always you can post some code here for us to look at, if that didn't help.
This is all I am doing. Its simply a hashmap with key as a string value and each key can have multiple string values. What I am finding is when I pass in my wordlist with id....if the wordlist happens to have duplicates that are in another key field that key field gets overwritten with below. So of course I am losing data.
That code doesn't tell me whether there's only one List object or many of them. (There should be many.) You're passing the List in from some code outside.
Having said that, I don't think the method you posted is particularly useful. The first part of the code does lazy initialization of the HashMap -- what for? Why not just create the HashMap at the start of the code which does the processing of the words? And once you get rid of that, all the method does is to call the "put" method which adds the List. So instead of calling "addToList(string, list)" you would call "wordlists.put(string, list)".
It also appears that you have a WordList class which is a subclass of List. Was there a reason you did that instead of just using, say, ArrayList<String>?
What I am finding is when I pass in my wordlist with id....if the wordlist happens to have duplicates that are in another key field that key field gets overwritten with below. So of course I am losing data.
Hay Steve , its quite expected behavior of a Map . If the Map gets the same key again then it will overwrite the old value with the new value .I am suggesting to read how HashMap work internally .