java 中list类型未知,列表< List<?>>和List< List>是java中不兼容的类型...

I did not get this code to compile either way:

List a = new ArrayList();

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 Set> getSubTypesOf(final Class 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>> 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>> 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

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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java,比较操作符>(大于)、<(小于)、>=(大于等于)和<=(小于等于)用于比较两个值的大小。这些操作符可以用于比较基本数据类型(如int、double等)以及字符串。当对基本数据类型进行比较时,它们会根据数值的大小返回一个布尔值(true或false)。比较字符串时,它们会根据字典顺序来确定字符串的大小。 引用提到的“>>>”运算符实际上不是比较操作符,而是位移操作符。它用于对整数进行无符号右移操作,将数值向右移动指定的位数,并在空出的高位填入0。该运算符不会区分正负数,所以它适用于处理无符号位移的情况,而不是比较大小。 综上所述,Java的比较操作符>、<、>=和<=用于比较两个值的大小,而“>>>”运算符用于进行无符号右移操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [java字符串转义,把<>转换成等字符【原】](https://blog.csdn.net/weixin_30570101/article/details/97061021)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [MyBatis sql< 和 > 转义操作符的用法](https://blog.csdn.net/qq_37855368/article/details/126609195)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Java 运算符 &与&& << >> >>>区别 ](https://blog.csdn.net/brian_huamm/article/details/40828161)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值