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

HashMap

Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
How do I insert the following into this HashMap?
HashMap hmap = new HashMap();
hmap.put("NY", "New York");
hmap.put("SF", "San Francisco");
When I do the above, I get identifier expected error.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Well, you shouldn't -- as long as you've imported java.util.* generally or the java.util.HashMap class explicitly.


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Did you import java.util.HashMap? Exactly which line number is referenced by the error message? What does the surrounding code look like? I'm guessing you've got this code declared outiside any method or constructor. A declaration like like HashMap hmap = new HashMap() would be OK here, but not a statement like hmap.put().


"I'm not back." - Bill Harding, Twister
Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
Yes, you were right. I had hashmap.put outside a method().I put it inside a method abd its working fine.
I had 1 more question-
How do I implement the following-
if(key="NY" exists in the hashmap)
return false
else
return true;
Also, can you suggest to me a good site giving lots of code examples and explanations for hashmap? Didn't seem to find any.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Follow your way to the JDK JavaDoc ... HashMap implements interface Map, which shows a couple methods containsKey and containsValue that ought to help you find whether something is already in the map.
This is a 1.3 link because the 1.4 link makes my IE deadly slow. No idea why. http://java.sun.com/j2se/1.3/docs/api/index.html


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
Thanks!! Will the HashMap only contain values in the following format-
(key, value)
or can it also have
(key, value1, value2, value3....);
I mean like-
("NY", "New York", "USA", Mayor's name)
What can I use to store a value and 3 objects in reference to that value as given above?
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
You can use an array...
Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
Thanks a lot. I tried it using arrays as well as vectors. They both work well.
Now say I have the following structure for a hashmap-
"AA", ("1861", "New York", "USA")
"BB", ("1920", "San Francisco", "USA")
"CC", ("1861", "Moscow", "Russia")
"DD", ("1987", "Moscow", "Russia")
"EE", ("2000", "Karachi", "Pakistan")
where the first element is a key and the second is a vector containing 3 elements, some imp. year in the history of the city, city name and country name.
I want to get the all keys for the hashmap given cityname = "Moscow"
That is, I want to get an array {"CC", "DD"} which contains the keys.
I'm working on HashMaps and just don't know how to proceed.
Can you please tell me what to do?
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Here is one way, not sure if it is best, but here goes. I call your hashmap object "map".

The resulting arraylist "keyList" should only contain the keys you are interested in.
Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
Given a HashMap is there a method to get the keys , not in the form of a set but 1 key at a time?
Iteration1: get "NY"
Iteration2:get "next key"...
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
That's what Kim just gave you - except of course it does use a Set, but so what? You get the keySet(), then you iterate through it. On each iteration, you're dealing with one and only one key.
Since you need both keys and values I'd suggest a slightly different techinque, also using a Set:

Essentially using entrySet() allows you to fetch both key and value at the same time. Well you still need to use getKey() and getValue(), but these are notably simpler and faster than the HashMap's get() method. So using entrySet() requires much less work from the JVM, as it doesn't need to look up the hash code for each entry in order to get the value.
----
posted January 26, 2004 10:55 AM Thanks a lot. I tried it using arrays as well as vectors. They both work well.
Now say I have the following structure for a hashmap-
"AA", ("1861", "New York", "USA")
...
where the first element is a key and the second is a vector containing 3 elements, some imp. year in the history of the city, city name and country name.

My guess is that you will be better off in the long run creating a class to hold the three elements in, e.g.

Since the three data elements presumably have completely different roles in processing, it will probably be useful to have a data structure which gives them names. Someone looking at a String[] array won't be able to tell what the strings mean, but someont looking at this class will have a much better idea.
----
What you're doing now is a reverse lookup of a Map - looking up keys by value. It's possible to do this as shown above. But if there are many entries, it can be slow. If you're going to do this sort of thing frequently, it may be useful for you to create a second (or third, fourth) HashMap which stores the keys and values in the reverse order. E.g. you can create a HashMap with city as the key, and each value is an ArrayList of the Records which refer to that city. To look up records by city, use this map. Then maybe have another HashMap using year as key (wrapped in an Integer) and each value is an ArrayList of Records which refer to that year. It will be more work to set up these other HashMaps initially, and you will need to be careful that any time you update one you also update the others. But the subsecquent lookups will be much easier.
You may also decide you want to use a TreeMap rather than a HashMap, especially for lookup by year, because TreeMap has methods which would allow you to look up a range of entries, e.g. all records before 1800, or between 1855 and 1891.
To be honest it sounds like you're new to using Maps, so the above techniques (multiple Maps and replacing HashMap with TreeMap) are ones you probably shouldn't try until you're a bit more confident in what you're doing. But they're things you might want to try later. Good luck.
Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
That was a wonderful solution, the thing about Set entries....
The whole picture has now become very clear.
Thanks a lot for your patient replies.
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Set entries = map.entrySet();
Wow, you learn something cool every day. Glad to have that info. I use Hashmaps often, but always relied on the get method, I'll have to give this method a try. Thanks for posting that info Jim.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMap