[英]Are there any advantages between List<CustomObject> and HashMap <String, Object>
I am trying to implement a solution (in Java 1.6) where i need to store some values (for a set of properties) and thinking in three options considering the following three (Any other idea is of course wellcome!) 我正在尝试实现一个解决方案(在Java 1.6中),我需要存储一些值(对于一组属性)并考虑以下三个思考三个选项(任何其他想法当然是好的!)
Option 1 选项1
Create a class (call it Property
) that can store different type of objects (String, int, boolean...) and and work with the set of properties as a List<Property>
创建一个类(称为
Property
),它可以存储不同类型的对象(String,int,boolean ...),并使用这组属性作为List<Property>
Something like: 就像是:
private String type; //Store the type of Object
private String name; //Store the name of the property
private String valueStr; //Store the String value
private int valueInt; //Store the int value
private boolean valueBool; //Store the boolean value
I dont really like the idea of having many properties and using only one of them. 我真的不喜欢拥有许多属性并只使用其中一个属性的想法。 (only one of the values will be set per property)
(每个属性只设置一个值)
Option 2 选项2
Use HashMap<String, Object>
and parse the type on each case. 使用
HashMap<String, Object>
并解析每种情况下的类型。
Have the good thing that you can get the Property
by name 有你可以通过名字获得
Property
的好处
Option 3 选项3
Use HashMap<String, Property>
Where the String is the name of the property and you can get the value with the name and no need to parse. 使用
HashMap<String, Property>
其中String是属性的名称,您可以使用名称获取值,而无需解析。
Questions are: Which of one you think is the best one? 问题是:您认为哪一个最好? or if none of them are good i would like to hear other ideas
或者如果它们都不好,我想听听其他想法
Also is there any performance difference between the List and the HashMap? List和HashMap之间是否有任何性能差异?
Thanks in advance for the help. 在此先感谢您的帮助。
I think better is to have a custom Value class like this: 我认为更好的是拥有这样的自定义Value类:
public class MyValue {
enum Type {
INT, STRING, BOOL;
}
private Type type; //Store the type of Object in Type Enum
private Object value; //Store the value in Object
public void setValue(int val) {
type = Type.INT;
value = new Integer(val);
}
public void setValue(String val) {
type = Type.STRING;
value = val;
}
public void setValue(boolean val) {
type = Type.BOOL;
value = new Boolean(val);
}
public String stringVal() {
// check type to be STRING first
return (String) value;
}
public int intVal() {
// check type to be INT first
return ((Integer) value.intValue());
}
public boolean booleanVal() {
// check type to be BOOL first
return ((Boolean) value.booleanValue());
}
}
You will need to convert from Object to specific type based on enum Type
in your getters. 您需要根据getter中的
enum Type
将Object从Object转换为特定类型。
Another option would be something like this, using inheritance rather than keeping a large number of unused fields around. 另一种选择是这样的,使用继承而不是保留大量未使用的字段。
public interface Property {
String getType();
String getName();
Object getValue();
}
public abstract class AbstractProperty implements Property {
private final String name;
protected AbstractProperty(String name) {
this.name = name;
}
}
public class StringProperty extends AbstractProperty {
private final String value;
public StringProperty(String name, String value) {
super(name);
this.value = value;
}
@Override
public String getType() {
return String.class.getName();
}
@Override
public String getValue() {
return value;
}
}
public class IntegerProperty extends AbstractProperty {
private final Integer value;
public IntegerProperty(String name, Integer value) {
super(name);
this.value = value;
}
@Override
public String getType() {
return Integer.TYPE.getName();
}
@Override
public Integer getValue() {
return value;
}
}
I think option 2 would be the best for you. 我认为选项2对你来说是最好的。 Considering that you are storing properties I am expecting that you would be querying this list quite often which again points in the direction of a HashMap as that would make your lookup very efficient.
考虑到你正在存储属性,我希望你会经常查询这个列表,它再次指向HashMap的方向,因为这会使你的查找非常有效。
I suggest using an enum
instead. 我建议改用
enum
。 Enums are good for holding lists of values, and are effective at retrieval. 枚举适用于保存值列表,并且在检索时有效。
public enum Property {
TYPE,
NAME,
VALUEINT; //...
private String sProp = "";
private int iProp = 0;
private boolean bProp = false;
public String getStringProp() {return sProp;}
public int getIntProp() {return iProp;}
public boolean getBoolProp() {return bProp;}
public void setStringProp(String str) {this.sProp = str;}
public void setIntProp(int i) {this.iProp = i;}
public void setBoolProp(boolean b) {this.bProp = b;}
}
This can then be accessed with Property.TYPE
, Property.VALUEINT
, etc. You can set properties with Property.TYPE.setStringProp()
, and get them with Property.TYPE.getStringProp()
. 然后可以使用
Property.TYPE
, Property.VALUEINT
等访问它。您可以使用Property.TYPE.setStringProp()
设置属性,并使用Property.TYPE.getStringProp()
获取它们。
You can read more about enums from Oracle's site . 您可以从Oracle网站上阅读有关枚举的更多信息。
I am unsure if there's one 'best' way. 我不确定是否有一种“最佳”方式。 It really depends on how the data would be used after storing in a data structure.
这实际上取决于在数据结构中存储后如何使用数据。
In cases when I just need to accumulate properties and do something on each of them, I'd use a list, or even an array, sometimes. 如果我只需要累积属性并对每个属性执行某些操作,有时我会使用列表,甚至是数组。
If you might have to get a particular property, say by name, then a HashMap could help. 如果您可能需要获取特定属性(例如名称),那么HashMap可以提供帮助。
Again if you want to use the native object type or an instance of Property depends on what kind of data you have. 同样,如果您想使用本机对象类型或Property的实例,则取决于您拥有的数据类型。
Which performs better depends on the number of objects you have, how you'd access them for use, how often you'd insert and several other factors. 哪个表现更好取决于你拥有的对象数量,你如何访问它们,你插入的频率和其他几个因素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.