Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap

 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you shouldn't -- as long as you've imported java.util.* generally or the java.util.HashMap class explicitly.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use an array...
 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic