Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Best hashmap to use for this situation

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?

thanks
 
Sheriff
Posts: 26957
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
steve kelly
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Paul Clapham
Sheriff
Posts: 26957
84
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>?
 
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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 .

Thanks ,
Satya
reply
    Bookmark Topic Watch Topic
  • New Topic