0

I apologize if the seems trivial but I always get confused on proper programming techniques. Sometimes there are too many ways to do one thing.

I have a HashMap <String, String> to store most of my data. I have a double that I would like to store in the same map. My question is:

If possible is it better to convert the non string to a string and store it in the current data structure; or would it be better to store a HashMap<String, Object> and cast the data accordingly when it gets used?

In this example would it be better to convert the Double to a String to store in the HashMap<String, String>?

Or, store all the data in a HashMap<String, Object> map and access it by casting data like this?

    Double dub = (Double) map.get(doublekey);

I am using this in Android development and was wondering which is the preferred method of doing things.

4
  • 1
    "Preferred" would be rethinking about how to approach this. Why do you have a random Double there? It defeats the purpose of generics since you're now mixing Types. Although, I would be more partial to String, if I had no other choice in implementation since it's more specific and strings are commonly used to keep other types serialized. Commented Feb 18, 2013 at 1:20
  • I agree with A-C, your approach shouldn't be to bend generics one way or the other. Commented Feb 18, 2013 at 1:21
  • 1
    It's certainly legit to make the HashMap be <String,Object> and do the casts. There are other approaches. (Originally there were no generics, and this wouldn't even be an issue. It's a bit disingenuous to whine that generics are being misused if you do it this way.) Commented Feb 18, 2013 at 2:16
  • it may depend on whether performance is an issue, and how many doubles you are storing. Double.parseDouble() is relatively pretty slow I would think Commented Feb 18, 2013 at 2:28

1 Answer 1

1

I would say there isn't a hard and fast rule here. Ideally you wouldn't have to make this choice. The real question is why do you have a random double that must be stored in your hash map? If this is a one-time thing then it's probably not a problem to just convert it to a string. If however, you anticipate many doubles as well as string which must be stored, perhaps a different approach would be best. Using the type Object will make things more difficult for you down the line because you can no longer assume there are only Strings in the hash map. If you do change it to Object make sure you carefully check for anywhere in your code which may assume that Object is a string.

Sign up to request clarification or add additional context in comments.

1 Comment

I apologize for not getting to this sooner. But this was a perfect answer. You asked questions of my code that i had not asked myself and pushed me to think about the design. Thankyou @Memento Mori

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.