简体   繁体   中英

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!)

Option 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>

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

Use HashMap<String, Object> and parse the type on each case.

Have the good thing that you can get the Property by name

Option 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.

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?

Thanks in advance for the help.

I think better is to have a custom Value class like this:

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.

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. 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.

I suggest using an enum instead. 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() .

You can read more about enums from Oracle's site .

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.

Again if you want to use the native object type or an instance of Property depends on what kind of data you have.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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