字典式数据结构.这是一个好习惯吗?

Jim*_*Jim 4 java generics map data-structures

我需要一个数据结构来存储不同类型的objects.Eg的String,Boolean和其他类.
是否使用了一个Map<String, Object>使用密钥的地方你得到了相应的对象,假定你知道如何把它作为一个好习惯?
有更好的解决方案吗?

Iva*_*lik 5

这是PropretyHolder我刚才写的一个完美的用例.您可以在我的博客上阅读相关内容.我开发时考虑了不变性,随意根据您的需求进行调整.

一般来说,如果你想从Java中的类型安全中获利,你需要知道你的密钥.我的意思是 - 几乎不可能开发出来自外部源的密钥类型安全解决方案.


这是一个知道其值类型的特殊键(它不完整请下载完整版本的源代码):

public class PropertyKey<T> {
    private final Class<T> clazz;
    private final String name;

    public PropertyKey(Class<T> valueType, String name) {
        this.clazz = valueType;
        this.name = name;
    }

    public boolean checkType(Object value) {
        if (null == value) {
            return true;
        }
        return this.clazz.isAssignableFrom(value.getClass());
    }

    ... rest of the class

}
Run Code Online (Sandbox Code Playgroud)

然后你开发一个利用它的数据结构:

public class PropertyHolder {

    private final ImmutableMap<PropertyKey<?>, ?> storage;

    /**
     * Returns value for the key of the type extending-the-one-declared-in-the {@link PropertyKey}.
     * 
     * @param key {@link PropertyKey} instance.
     * @return Value of the type declared in the key.
     */
    @SuppressWarnings("unchecked")
    public <T extends Serializable> T get(PropertyKey<T> key) {
        return (T) storage.get(key);
    }

    /**
     * Adds key/value pair to the state and returns new 
     * {@link PropertyHolder} with this state.
     * 
     * @param key {@link PropertyKey} instance.
     * @param value Value of type specified in {@link PropertyKey}.
     * @return New {@link PropertyHolder} with updated state.
     */
    public <T> PropertyHolder put(PropertyKey<T> key, T value) {
        Preconditions.checkNotNull(key, "PropertyKey cannot be null");
        Preconditions.checkNotNull(value, "Value for key %s is null", 
                key);
        Preconditions.checkArgument(key.checkType(value), 
                "Property \"%s\" was given " 
                + "value of a wrong type \"%s\"", key, value);
        // Creates ImmutableMap.Builder with new key/value pair.
        return new PropertyHolder(filterOutKey(key)
                .put(key, value).build());
    }

    /**
     * Returns {@link Builder} with all the elements from the state except for the given ket.
     * 
     * @param key The key to remove.
     * @return {@link Builder} for further processing.
     */
    private <T> Builder<PropertyKey<? extends Serializable>, Serializable> filterOutKey(PropertyKey<T> key) {
        Builder<PropertyKey<? extends Serializable>, Serializable> builder = ImmutableMap
                .<PropertyKey<? extends Serializable>, Serializable> builder();
        for (Entry<PropertyKey<? extends Serializable>, Serializable> entry : this.storage.entrySet()) {
            if (!entry.getKey().equals(key)) {
                builder.put(entry);
            }
        }
        return builder;
    }

    ... rest of the class

}
Run Code Online (Sandbox Code Playgroud)

我在这里省略了很多不必要的细节,如果不清楚,请告诉我.