java中泛型错误_从Java中的泛型类型推断泛型类型(编译时错误)

我有一个静态函数,其泛型类型T具有以下签名

public static List sortMap(Map map)

应返回带有某些属性的地图键列表.

现在我想传递S类型的通用HashMap

Map map

调用泛型类中的静态函数,该类将映射作为成员变量.

我在下面列出了一个最小的代码示例.

但是,我收到一条错误消息(S和T都是T,但在我的代码的不同范围内,即T#1 = T,T#2 = S):

required: Map

found: Map

reason: cannot infer type-variable(s) T#1

(argument mismatch; Map cannot be converted to Map)

怎么解决这个问题?我很惊讶Java不允许从泛型类型推断泛型类型. Java中的哪种结构可以用来处理那种更抽象的代码推理?

码:

public class ExampleClass {

Map map;

public ExampleClass () {

this.map = new HashMap();

}

//the following line produces the mentioned error

List sortedMapKeys = UtilityMethods.sortMap(map);

}

public class UtilityMethods {

public static List sortMap(Map map) {

// sort the map in some way and return the list

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值