泛型通配符无法转换为 Generictype
Generics wildcard isnt capable to cast to Generictype
我遇到了一个我找不到解决方案的问题。
我经常使用一个映射,其中键和值是具有匹配泛型类型的对象。对于每一对,泛型应该匹配。尽管条目之间的通用类型可能会有所不同。 (为清楚起见,我将包括一个示例)。
这可以通过使用通配符轻松实现。尽管正因为如此,您不能将键或值与彼此结合使用。
考虑底部包含的示例。没有(简单的)方法可以将 运行 的映射修改为 Cast 异常。尽管我仍然无法像在 useEntries()
中尝试的那样使用地图。所以我的问题是,是否有解决方法?提前致谢!
public class GenericWildcardTest
{
static Map<GenericObject<?>, Function<?, ?>> map = new HashMap<>();
public static <S> void put(GenericObject<S> genericObject, Function<S, S> function)
{
map.put(genericObject, function);
}
public static void useEntries()
{
for(Entry<GenericObject<?>, Function<?, ?>> currentEntry : map.entrySet())
//The #apply(); part simply wont compile because of cast errors.
currentEntry.getKey().set(currentEntry.getValue().apply(currentEntry.getKey().get()));
}
// Simple Object with generic.
static class GenericObject<T>
{
private T object;
public GenericObject(T object)
{
this.object = object;
}
public void set(T object)
{
this.object = object;
}
public T get()
{
return this.object;
}
}
}
你可以重写useEntries
方法如下:
@SuppressWarnings("unchecked")
public static void useEntries() {
for (Map.Entry<GenericObject<?>, Function<?, ?>> currentEntry : map.entrySet()) {
GenericObject key = currentEntry.getKey();
Function value = currentEntry.getValue();
key.set(value.apply(key.get()));
}
}
从方法中的 GenericObject
和 Function
中删除泛型将允许您对纯 Object
实例进行调用。然后您有责任确保正确输入。注释 SuppressWarning
将删除否则将打印的编译警告。
以下是您可以通过转换实现的方法:
@SuppressWarnings("unchecked")
public static <S> void useEntries() {
for(Entry<GenericObject<?>, Function<?, ?>> currentEntry : map.entrySet()) {
GenericObject<S> key = (GenericObject<S>)currentEntry.getKey();
Function<S, S> value = (Function<S, S>)currentEntry.getValue();
key.set(value.apply(key.get()));
}
}
此答案假设您的地图确实包含 Function<S, S>
,而不是 Function<GenericObject<S>, S>
。
我遇到了一个我找不到解决方案的问题。
我经常使用一个映射,其中键和值是具有匹配泛型类型的对象。对于每一对,泛型应该匹配。尽管条目之间的通用类型可能会有所不同。 (为清楚起见,我将包括一个示例)。 这可以通过使用通配符轻松实现。尽管正因为如此,您不能将键或值与彼此结合使用。
考虑底部包含的示例。没有(简单的)方法可以将 运行 的映射修改为 Cast 异常。尽管我仍然无法像在 useEntries()
中尝试的那样使用地图。所以我的问题是,是否有解决方法?提前致谢!
public class GenericWildcardTest
{
static Map<GenericObject<?>, Function<?, ?>> map = new HashMap<>();
public static <S> void put(GenericObject<S> genericObject, Function<S, S> function)
{
map.put(genericObject, function);
}
public static void useEntries()
{
for(Entry<GenericObject<?>, Function<?, ?>> currentEntry : map.entrySet())
//The #apply(); part simply wont compile because of cast errors.
currentEntry.getKey().set(currentEntry.getValue().apply(currentEntry.getKey().get()));
}
// Simple Object with generic.
static class GenericObject<T>
{
private T object;
public GenericObject(T object)
{
this.object = object;
}
public void set(T object)
{
this.object = object;
}
public T get()
{
return this.object;
}
}
}
你可以重写useEntries
方法如下:
@SuppressWarnings("unchecked")
public static void useEntries() {
for (Map.Entry<GenericObject<?>, Function<?, ?>> currentEntry : map.entrySet()) {
GenericObject key = currentEntry.getKey();
Function value = currentEntry.getValue();
key.set(value.apply(key.get()));
}
}
从方法中的 GenericObject
和 Function
中删除泛型将允许您对纯 Object
实例进行调用。然后您有责任确保正确输入。注释 SuppressWarning
将删除否则将打印的编译警告。
以下是您可以通过转换实现的方法:
@SuppressWarnings("unchecked")
public static <S> void useEntries() {
for(Entry<GenericObject<?>, Function<?, ?>> currentEntry : map.entrySet()) {
GenericObject<S> key = (GenericObject<S>)currentEntry.getKey();
Function<S, S> value = (Function<S, S>)currentEntry.getValue();
key.set(value.apply(key.get()));
}
}
此答案假设您的地图确实包含 Function<S, S>
,而不是 Function<GenericObject<S>, S>
。