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 question

 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a set of weather data, like wind speed, temp, pressure, etc. The data is a combination of ints, doubles and Strings. I want to share this data with other classes and access it by the name/key so I thought I would use a HashMap. Being new, all the examples I see define the values as a specific type. I found an example and modified it to what I want and it works. I am wondering though if this is the best approach.



I know there are other ways to loop through this code but wanted with something I could get working quickly as a test.

Thoughts?

Thanks
Mike
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My first thought is that you have at least two distinct data types in the hashmap (strings and numbers)... this will create issues at some point.

If you're always gathering the same data, why not just create a class that encapsulates a set of data? Instead of an essentially random, ad-hoc data structure (key/value pairs), use Java and object orientation for what it's designed for.
 
Tim McGuire
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have a set of weather data, like wind speed, temp, pressure, etc. The data is a combination of ints, doubles and Strings. I want to share this data with other classes and access it by the name/key so I thought I would use a HashMap. Being new, all the examples I see define the values as a specific type. I found an example and modified it to what I want and it works. I am wondering though if this is the best approach.


wouldn't it be better to have a "Weather" class that had private fields for each of your variables? and then have getters and setters for each when another class needs to read it? That is the way I've seen it done, anyway.

Other classes would then call weather.getPressure(); or weather.getHumidity();
If you wanted to quickly "loop" through or otherwise retrieve all the values, then you could have that function in your Weather object's toString().

If you think about it, having a HashMap for weather data wouldn't scale too well. what happens when you need weather for a different day?
 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I am headed to using a weather class. I had thought of doing that and this confirms this completely, the HashMap was a rabbit trail

Thanks
Mike
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Bates wrote:Okay, I am headed to using a weather class. I had thought of doing that and this confirms this completely, the HashMap was a rabbit trail

Thanks
Mike


It's a worthwhile idea for some situations (i.e., Java properties), but in those situations usually the data held is of the same type.
 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured based on the comments hashMap is not the solution. Reading through the more documentation to find good reasons to use HashMaps.

Thanks
Mike
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's a huge number of reasons applications might need key/value pairs, this just isn't one of them.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic