gpt4 book ai didi

java - 多级泛型类型的返回值不能分配给扩展类型

转载 作者:行者123 更新时间:2023-12-03 03:25:07 24 4
gpt4 key购买 nike

我有这个方便的方法(我已经使用了很多年,没有任何问题)。它只是转换 ListMap<SomeKey, List> ,按关键属性对它们进行分组。

为了避免不必要的转换,我将键属性作为字符串(引用方法名称)传递,并且还指定了该属性的类型。

@SuppressWarnings({"unchecked"})
@Nullable
public static <K, E> Map<K, List<E>> getMultiMapFromList(Collection<E> objectList, String keyAttribute, Class<K> contentClass)
{
// creates a map from a list of objects using reflection
...
}

上述方法在许多应用中多年来一直完美运行。但今天下面的案例提出了一个问题:

List<? extends MyBean> fullBeanList = getFullBeanList();

Map<MyKey, List<? extends MyBean>> multiMap;

// the following line doesn't compile.
multiMap = Utils.getMultiMapFromList(fullBeanList, "key", MyKey.class);

在开发过程中,我的 IntelliJ IDE 没有任何警告。但在编译过程中会出现:

Error:(...,...) java: incompatible types: java.util.Map<mypackage.MyKey, java.util.List<capture #2 of ? extends mypackage.MyBean>> cannot be converted to java.util.Map<mypackage.MyKey, java.util.List<? extends mypackage.MyBean>>

但我无法弄清楚这一点。我猜这与? extends有关。但我没有看到任何违规行为。我还想知道为什么它只出现在编译时?我认为,由于类型删除,一旦编译就不再重要了。

我确信我可以通过添加一些强制转换来强制执行此操作,但我想了解这里发生的情况。

编辑:

为了方便:

Test.java

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class Test
{
public static void main(String[] args)
{
List<? extends MyBean> input = new ArrayList<>();

Map<MyKey, List<? extends MyBean>> output;
output = test(input, MyKey.class); // doesn't compile
}

public static <K, E> Map<K, List<E>> test(Collection<E> a, Class<K> b)
{
return null;
}

private static class MyKey{}
private static class MyBean{}
}

编辑2

继续疯狂地前进:

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class Test
{
public static void main(String[] args)
{
List<? extends Number> input = new ArrayList<>();

// compiles fine
List<? extends Number> output1 = test1(input);

// doesn't compile
Map<String, List<? extends Number>> output2 = test2(input);
}

public static <E> List<E> test1(Collection<E> a) { return null;}
public static <E, K> Map<K, List<E>> test2(Collection<E> a) { return null;}
}

我不知道对此有何看法。只要我使用 1 级泛型,它就可以正常工作。但是当我使用 2 级泛型(即泛型中的泛型,例如 Map<K,List<V>> )时,它会失败。

最佳答案

这将解决您的问题。

您必须如下更改方法测试。

public static <K, E> Map<K, List<? extends E>> test(
Collection<? extends E> a, Class<K> b) {
return null;
}

问题是您没有告诉传递给方法的 ?s,并且在 Java 中它们不能保证相同。使该方法通用,以便您可以引用通用类型参数,并且在整个方法中保持相同。

下面是代码。

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class Test {
public static void main(String[] args) {
List<? extends MyBean> input = new ArrayList<>();

Map<MyKey, List<? extends MyBean>> output;
output = test(input, MyKey.class); // doesn't compile
}

public static <K, E> Map<K, List<? extends E>> test(
Collection<? extends E> a, Class<K> b) {
return null;
}

private static class MyKey {
}

private static class MyBean {
}

}

关于java - 多级泛型类型的返回值不能分配给扩展类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37722212/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com