Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hash Map Query

 
Marie O' Driscoll
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am editing open source code. A hash map is defined as follows:



Junction is a class with a string id and two float values - x and y. In a subsequent class i need to access the values in the hash map. I have the following but I'm getting NULL values: Can someone please point out how I should retrieve class info from the hash map?



I also tried the following but it didn't work either:



Many thanks in advance
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like this


Also, using public instance fields is horrid. make id, x, y private and create accessor and mutator methods.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides keySet() which returns all the keys, there are also methods values() which returns the values and entrySet() which returns both:
Prefer using either values() if you don't need the keys or entrySet() if you do need the keys inside your loop body. Using entrySet() is faster than using keySet() and get() since you don't need to do a full lookup - the value is already there.
 
Marie O' Driscoll
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for both replies - Problem solved!

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic