简体   繁体   English

从HashMap中提取值<String, Object>

[英]Extracting values from HashMap<String, Object>

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. 我的程序读取这些值,并将它们放入HashMap,其中“键”是文件中的参数,而值是对象。 This HashMap is then available to be referenced in the program and the changed values are eventually re-written out to the file. 然后,可以在程序中引用该HashMap,并且最终将更改后的值重新写到文件中。 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. 选择HashMap是因为一些值是字符串,其他值是整数,双精度数和最终列表。 Conversion from string to int, double, etc is performed when the values are put in the Map. 将值放在Map中时,将执行从字符串到int,double等的转换。 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. 当从HashMap中提取数据时,我的代码开始看起来非常混乱,因为现在我必须强制转换所有内容或进行转换。 Even the simple String (which comprise about 90% of my values in the file) looks bad. 即使是简单的String(在文件中约占我的值的90%)看起来也很糟糕。 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:

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: 现在我知道可以将parmsMap更改为:

HashMap<String, String> parmsMap = new HashMap<String, String>();

and 90% of my calls would be: 而我90%的电话是:

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? 并且一个在本质上比另一个更“安全”吗?

Don't use the HashMap directly. 不要直接使用HashMap 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. 基本上,提供与ResultSet相似的类型化方法。

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 (eg Class@4324fas)). 我认为最好的方法是使用instanceof运算符检查类的类型,并根据其执行适当的操作(使用您的方法可能会抛出ClassCastException或null指针异常,而且,并非所有的类都具有toString()方法如果将用户定义类的实例作为值,并且没有覆盖toString(),则它将显示没有任何含义的字符串(例如Class @ 4324fas)。 Then again, if you have too many instanceof checks then your code will look messy (too many if.. else if conditions). 再一次,如果您有太多的instanceof检查,那么您的代码将显得凌乱(如果条件太多,则if..else)。 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. 对于上面的示例,我参考了这个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM