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?
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?
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?
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?
author and iconoclast
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?
Joined: Oct 03, 2005
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?
author and iconoclast
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.