0

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?

Linus12
  • 43
  • 6

2 Answers2

1

Don't use the HashMap directly. Wrap it with a class that implements methods like:

public String getString(String name);
public int    getInt   (String name);
public double getDouble(String name);

Basically, provide typed methods similar to how ResultSet do it.

Andreas
  • 154,647
  • 11
  • 152
  • 247
  • I was actually trying to simplify things by using the HashMap directly, but on further evaluation, this makes more sense in this context. I should also be able to copy/modify this class for use in other programs with minimal changes. (field name changes and types etc.) Thanks – Linus12 Dec 08 '15 at 20:07
0

I think best way here would be to check the class type using instanceof operator and the perform the appropriate action based on it (using your approach may throw a ClassCastException or a null pointer exception, also, not all the class will have toString() method implemented. If you put the instance of a user defined class as a value and that doesn't have toString() overridden then it will show the string without any meaning (e.g. Class@4324fas)). Then again, if you have too many instanceof checks then your code will look messy (too many if.. else if conditions). Here's an example of how to do it via enum.

public A() {

    CLAZZ z = CLAZZ.valueOf(this.getClass().getSimpleName());
    switch (z) {
    case A:
        doA();
        break;
    case B:
        doB();
        break;
    case C:
        doC();
        break;
    }
}


enum CLAZZ {
    A,B,C;

}

For the above example, I took the reference of this so answer.

Community
  • 1
  • 1
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • My apologies, but I don't immediately see how this solves my problem. The parm values in the file can be added to in the future, and many types may not be known. But those parms that are utilized by the program have known types. I.e. "title" will always be a string, so we know what type of data "should" be in the value. The issue is "nicely" extracting that data to be passed to simple methods requesting simple parameter values. – Linus12 Dec 05 '15 at 02:27
  • As @Andreas has mentioned, we can use libraries like MapUtils. More on it here: https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/MapUtils.html – Darshan Mehta Dec 06 '15 at 00:29