java嵌套类型,在Java中混合嵌套类型参数和通配符

本文探讨了在Java中尝试编译带有泛型的公共类时遇到的错误,具体表现为方法参数类型不匹配。问题源于传入的HashMap无法转换为指定的泛型Map。解决方案是使方法接受更通用的Map参数类型,以消除类型转换错误。
摘要由CSDN通过智能技术生成

Why does trying to compile

public class GenericsFail {

public static void main(String[] args) {

accept(new HashMap>());

}

public static void accept(Map> multiMap) {}

}

give the error

GenericsFail.java:7: error: method accept in class GenericsFail cannot be applied to given types;

accept(new HashMap>());

^

required: Map>

found: HashMap>

reason: actual argument HashMap> cannot be converted to Map> by method invocation conversion

The wildcard is only allowed if it's not nested inside List.

解决方案

To be more general

void accept(Map> multiMap)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值