Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 列表<;CustomObject>;和HashMap<;字符串,对象>;_Java_List_Hashmap - Fatal编程技术网

Java 列表<;CustomObject>;和HashMap<;字符串,对象>;

Java 列表<;CustomObject>;和HashMap<;字符串,对象>;,java,list,hashmap,Java,List,Hashmap,我正在尝试实现一个解决方案(在Java 1.6中),在这个解决方案中,我需要存储一些值(对于一组属性),并考虑以下三个选项(当然,任何其他想法都是好主意!) 选项1 创建一个类(称之为Property),该类可以存储不同类型的对象(String、int、boolean…),并将属性集用作列表 比如: private String type; //Store the type of Object private String name; //Store the name of the proper

我正在尝试实现一个解决方案(在Java 1.6中),在这个解决方案中,我需要存储一些值(对于一组属性),并考虑以下三个选项(当然,任何其他想法都是好主意!)

选项1

创建一个类(称之为
Property
),该类可以存储不同类型的对象(String、int、boolean…),并将属性集用作
列表

比如:

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
我真的不喜欢拥有许多财产,却只使用其中一项的想法。(每个属性仅设置一个值)

选项2

使用
HashMap
并分析每个案例的类型

您可以通过名称获得
属性,这是一件好事

选项3

使用
HashMap
,其中字符串是属性的名称,您可以使用名称获取值,无需解析

问题是: 你认为哪一个是最好的? 或者,如果没有一个是好的,我想听听其他的想法

列表和HashMap之间是否存在性能差异


提前感谢您的帮助。

我认为最好有这样一个自定义值类:

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());
    }
}

您需要根据getter中的
枚举类型将对象转换为特定类型。

另一个选项类似于此,使用继承而不是保留大量未使用的字段

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;
    }
}

我认为选择2对你来说是最好的。考虑到您正在存储属性,我希望您会经常查询此列表,它再次指向HashMap的方向,因为这将使您的查找非常高效。

我建议改用
枚举。枚举非常适合保存值列表,并且在检索时非常有效

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;}
}
然后可以使用
Property.TYPE
Property.VALUEINT
等访问此属性。您可以使用
Property.TYPE.setStringProp()
设置属性,并使用
Property.TYPE.getStringProp()
获取属性


您可以从中阅读有关枚举的更多信息。

我不确定是否有一种“最佳”方法。这实际上取决于数据存储在数据结构中后如何使用

如果我只需要累积属性并对每个属性执行某些操作,有时我会使用列表,甚至数组

如果您可能需要获取一个特定的属性,比如通过名称,那么HashMap可能会有所帮助

同样,如果要使用本机对象类型或属性实例,则取决于您拥有的数据类型


哪个性能更好取决于您拥有的对象数量、访问对象的方式、插入的频率以及其他几个因素

最好是一个单独的
属性
对象,该对象具有与每个属性相对应的正确类型字段。您是否正在尝试为新结构创建类似元数据结构的内容?如果是的话,我会选择选项2,但仍然想知道为什么你需要这样的东西?谢谢大家的回答、想法和评论。我真的很喜欢两个答案@anubhava和@280Z28。您将值放入了enum。感谢您的想法,类似于@anubhava answer。我喜欢带有enum的类型的想法,并且允许您在没有类外铸件的情况下工作。感谢您在本案例中的努力。感谢您花时间回答。感谢Thaks花时间思考以最好的方式完成此任务。