![](/img/trans.png)
[英]Hibernate how to map Enum key and value in Map<enum,enum> as a String
[英]Map with enum key and different value types
我想在Java中定义映射,哪些键是枚举,以及值的类型取决于键。 例如,假设我们有以下枚举类型:
enum KeyType {
HEIGHT(Integer.class),
NAME(String.class),
WEIGHT(Double.class)
// constructor and getter for Class field
}
和一些地图:
Map< KeyType, Object > map = new EnumMap<>(KeyType.class);
是否有任何简单而安全的方法来编写泛型方法:
public < T > T get(KeyType key) {
//...
}
从该地图获取价值并将其转换为与类型对应的值?
更新!!!:记住这一点:
enum KeyType {
//your enums ...
private final Class val;
//constructor ...
//and generic(!) access to the class field:
<T> Class<T> val() {
return val;
}
}
...这个有可能:
public <T> T get(KeyType key) {
return (T) key.val().cast(map.get(key));
}
您的地图定义需要
Map< KeyType, ?> map = new EnumMap<>(KeyType.class);
如果将Object
指定为泛型类型,则只允许实际的Object
实例,而不是子类型。
我不相信有任何直接的,通用的方式(没有双关语意)去做你想要的。 您需要创建一些映射函数,该函数根据枚举将对象转换为正确的类型。
你不能用枚举来做。 但是你可以编写一个“假的”枚举(Java代码在Java 1.5之前使用私有构造函数和公共静态实例的方式),并为每个常量附加一个泛型类型:
import java.io.Serializable;
import java.util.Map;
public final class KeyType<T>
implements Serializable {
private static final long serialVersionUID = 1;
public static final KeyType<Integer> HEIGHT =
new KeyType<>("HEIGHT", Integer.class);
public static final KeyType<String> NAME =
new KeyType<>("NAME", String.class);
public static final KeyType<Double> WEIGHT =
new KeyType<>("WEIGHT", Double.class);
private static final KeyType<?>[] allValues = {
HEIGHT, NAME, WEIGHT
};
/** @serial */
private final String name;
/** @serial */
private final Class<T> type;
private KeyType(String name,
Class<T> type) {
this.name = name;
this.type = type;
}
public String name() {
return name;
}
public Class<T> getType() {
return type;
}
@Override
public String toString() {
return name();
}
public static KeyType<?>[] values() {
return allValues.clone();
}
public static KeyType<?> valueOf(String name) {
for (KeyType<?> value : allValues) {
if (value.name.equals(name)) {
return value;
}
}
throw new IllegalArgumentException("No such value: \"" + name + "\"");
}
@Override
public boolean equals(Object obj) {
return (obj instanceof KeyType &&
this.name.equals(((KeyType<?>) obj).name));
}
@Override
public int hashCode() {
return name.hashCode();
}
public T getValue(Map<KeyType<?>, ?> map) {
return type.cast(map.get(this));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.