Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

put() method

 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have more than 500 film titles to save to disk in a HashMap. I am finding it tedious to repeat the below statement with the put() method. Instead, I wrote a for loop which tests to see if a given key, Film 1 etc, is in the HashMap. If not, the next number is assigned to the variable film1. The code with the for loop is after the broken line. Does anyone know whether the for loop will actually do what I have described?

 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should start your post with a problem description -- what are you trying
to do, what is you goal? From the sound of it, having
contiguous keys "film1", "film2", ..., I wonder why you want a Map
at all -- why not use a List?
 
Norm Radder
Ranch Hand
Posts: 728
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>whether the for loop will actually do

One quick way is to execute the code and see. Do it for 10 vs 1000 for testing.

Are all the values in the container supposed to be the same? Your code creates many unique keys all of which have the save value!
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henri --

At the end of the other thread, I thought you understood how to use a Map, but now it's looking like you didn't. If you want to look up a film by it's title, then the key has to be the title, and the value has to be the film object. The code you've shown here uses the eight strings "Film 1" ... "Film 8" as the keys, and stores the name of one film as the value.

You don't say where the film data is coming from, and you don't say what you're trying to do now with the "Film 1" strings, so I really can't tell you exactly what to do. Can you explain in general terms what this code is supposed to do, and then we can tell you how to write it?

Or better yet, do you understand what a Map does? Can you explain it?
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your feedback, it is really appreciated. I have never used a HashMap. I had thought that the key would be the film number, and the value would be the title. I think the key, if it is the title will be accociated with the bean. Am I right?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code you show here seems to be doing the right thing, but it's totally different from the code you showed last. Is this change deliberate?
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest. I have begun to enter the film titles and it works. I was complicating things alot for nothing. I find Java very intimidating. I had one last question. I have set the capacity of the Hashmap to 1000. Does this affect memory?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generally you should just leave the capacity alone, and accept the default. It only affects the initial memory/speed tradeoff; over time, as much memory is allocated as is needed, anyway, no matter what the initial capacity setting is.
 
henri henri
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok thanks for the tip...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic