File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes put() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "put() method" Watch "put() method" New topic
Author

put() method

henri henri
Ranch Hand

Joined: Oct 03, 2005
Posts: 115
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

Joined: Sep 16, 2005
Posts: 1780
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?


There is no emoticon for what I am feeling!
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 687
    
    1
>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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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?


[Jess in Action][AskingGoodQuestions]
henri henri
Ranch Hand

Joined: Oct 03, 2005
Posts: 115
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Oct 03, 2005
Posts: 115
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Oct 03, 2005
Posts: 115
Ok thanks for the tip...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: put() method