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 think in three options, given the following three (any other idea, of course, is welcome!)
Option 1
Create a class (name it Property ) that can store various types of objects (String, int, boolean ...) and work with a set of properties as List<Property>
Sort of:
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 do not like the idea of having many properties and using only one of them. (for each property only one of the values will be set)
Option 2
Use a HashMap<String, Object> and parse the type in each case.
Good thing you can get Property by name
Option 3
Use HashMap<String, Property> Where String is the name of the property, and you can get the value with the name and not need parsing.
Questions: Which one do you think is the best? or if none of them are good, I would like to hear other ideas.
Also, is there a performance difference between a list and a HashMap?
Thanks in advance for your help.
I think it's best to have your own Value class as follows:
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 a specific type based on enum Type in your getters.
Another option would be something like this, using inheritance rather than storing a large number of unused fields.
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 suggest using enum instead. Enumerations are useful for storing lists of values and are effective in searching.
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;} } After that, you can access using Property.TYPE , Property.VALUEINT , etc. You can set properties using Property.TYPE.setStringProp() and get them using Property.TYPE.getStringProp() .
Learn more about transfers from the Oracle website .
I am not sure if there is a “better” way. It depends on how the data will be used after storage in the data structure.
In cases where I just need to accumulate properties and do something on each of them, sometimes I would use a list or even an array.
If you may need to get a specific property, say, by name, then HashMap can help.
Again, if you want to use your own object type or property instance, it depends on what data you have.
What works best depends on the number of objects that you have, how you turn to them for help, how often you insert and several other factors.