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 question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "HashMap question" Watch "HashMap question" New topic
Author

HashMap question

Mike Bates
Ranch Hand

Joined: Sep 19, 2009
Posts: 81
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Apr 30, 2003
Posts: 820


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

Joined: Sep 19, 2009
Posts: 81
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

Joined: Feb 23, 2007
Posts: 1561
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

Joined: Sep 19, 2009
Posts: 81
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

Joined: Sep 29, 2008
Posts: 12617

There's a huge number of reasons applications might need key/value pairs, this just isn't one of them.
 
 
subject: HashMap question
 
Similar Threads
Sorting a set of arrays
Create hashmap and dynamically populate
Clarification regarding HashMap
Generating Unique Random Numbers in JAVA
same key for diff values in HashMap