I have a set of parameters, stored in a file as strings in the form of:
param = value
My program reads these values and puts them into a HashMap where the "key" is the param from the file and the value is the object. This HashMap is then available to be referenced in the program and the changed values are eventually re-written out to the file. Access is fast (as opposed to disk access, as it is run on some very old (and slow) hardware.
The HashMap was chosen because some of the values are Strings, others are Integers, Doubles, and eventually lists. Conversion from string to int, double, etc is performed when the values are put in the Map. This allows for upfront checking since the parameter files are usually created by hand at first, and may be modified by hand from time to time.
When pulling the data from the HashMap, my code starts to look really messy because now I have to either Cast everything, or convert it. Even the simple String (which comprise about 90% of my values in the file) looks bad. For example, if I have method that performs some processing on a string:
public void pString(String x) {
// do something with 'x'
}
and I have the following definitions:
HashMap<String, Object> parmsMap = new HashMap<String, Object>();
parmsMap.put("wait", 300); // integer value
parmsMap.put("title", "File Access"); // string value
I have to call pString in one of the following ways:
pString((String)parmsMap.get("title"));
pString(parmsMap.get("title").toString());
Neither of these is fairly clear!
Now I know I could change the parmsMap to just be:
HashMap<String, String> parmsMap = new HashMap<String, String>();
and 90% of my calls would be:
pString(parmsMap.get("title"));
which is much less messy.
BUT, that means I would have to check/convert all my "non String" entries every time I wanted to access them!
Is there a better way? If not, which method would take less of performance hit?
pString((String)parmsMap.get("title"));
or
pString(parmsMap.get("title").toString());
And is one more inherently "safer" than the other?