是java中不兼容的类型

是java中不兼容的类型

本文介绍了列表< List<?>>和List< List>是java中不兼容的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有得到这个代码以任何方式编译:

  List< List& a = new ArrayList(); 
List< List<?>> b = new ArrayList();

a = b; //不兼容类型
b = a; //不兼容的类型

看来java不会考虑 List 列表<?> 在泛型时是相同的类型。



就是它?



上下文



有一个具有以下签名的库函数: public< T>设置< Class< ;?延伸T> getSubTypesOf(final Class< T> type)。这对于作为参数传递的简单类型工作正常,但在泛型的情况下,结果不是使用通配符参数化,导致javac抱怨原始类型。我想把结果传播到我的应用程序的其余部分 Set< Class< ;?扩展了GenericTypeHere<?>>> ,但是简单的强制类型无法正常工作。



EDIT:Solution

$感谢您的答案,以下是我如何使它工作到最后:

  @SuppressWarnings({rawtypes,unchecked})
private static Set< Class< ;? extends GenericTypeHere<?>>>> factoryTypes(){
return(set)new Reflections(...)。getSubTypesOf(GenericTypeHere.class);
}


解决方案

到微妙的语义差异。

 列表


$ b b

这是 List 的原始类型,等于 T 的类型为 Object 。所以它就像说:

  List< Object> 



现在,编译器知道一个事实,无论发生什么,这是类型 Object

  List myList = new ArrayList(); 
myList.add(new Object());

它会正常工作!这是因为 Object 是相同的或者它是类型的一些派生。

 列表<?> 

这是字面上的未知列表()。我们甚至不知道这里的东西的子类是 Object 类型。事实上,类型是一个未知类型。它与 Object 无关!这是为什么当你尝试做。

 列表<?> myList = new ArrayList<?>(); 
myList.add(new Object());

你得到一个编译时错误!


I did not get this code to compile either way:

List<List> a = new ArrayList();
List<List<?>> b = new ArrayList();

a = b; // incompatible types
b = a; // incompatible types

It seems that java does not consider List and List<?> to be the same type when it comes to generics.

Why is that? And is there some nice way out?

Context

There is a library function with following signature: public <T> Set<Class<? extends T>> getSubTypesOf(final Class<T> type). This works fine for simple types passed as argument but in case of generics the result is not parametrized with wildcard causing javac to complain about raw type. I would like to propagate the result to the rest of my application as Set<Class<? extends GenericTypeHere<?>>> but simple cast does not work as I expect.

EDIT: Solution

Thanks for the answers, here is how I get it working in the end:

@SuppressWarnings({"rawtypes", "unchecked"})
private static Set<Class<? extends GenericTypeHere<?>>> factoryTypes() {
    return (Set) new Reflections("...").getSubTypesOf(GenericTypeHere.class);
}
解决方案

Okay, so this is due to a subtle semantic difference.

List

This is the raw type of List, which equates to T being of type Object. So it's the same as saying:

List<Object>

Now, the Compiler knows for a fact, that whatever happens, this is a subclass of type Object. And if you do..

List myList = new ArrayList();
myList.add(new Object());

It will work fine! This is because Object is the same or it is some derivation of the type.

List<?>

This is literally a list of unknown (Java Docs). We don't even know that the subclass of the things in here are of type Object. In fact, the ? type is an unknown type all on its own. It has nothing to do with Object! This is why when you try and do..

List<?> myList = new ArrayList<?>();
myList.add(new Object());

You get a compile time error!

这篇关于列表&lt; List&lt;?&gt;&gt;和List&lt; List&gt;是java中不兼容的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 14:05