• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

HashMap question

 
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
 
Author
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.
 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • 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
 
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
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.
 
today's feeble attempt to support the empire
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic