如何将通用参数绑定到特定的List子类? [英] How to bind a generic parameter to a specific List subclass?

查看:67
本文介绍了如何将通用参数绑定到特定的List子类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public static< K,D extends List< T>,T>列表< Map< K,D>> meth(K k,D d){
List< Map< K,D>> daBytes =(List >)
new ArrayList< Map< K,List< List< Byte>>>>();
// ...
}

编译器找出 D List< List<>> 和/或 T 是一个< List<>< / code> - 并且实际上摆脱了剧组。取而代之的是:

不能从ArrayList转换< Map< K,List< List< Byte>>>>到< Map< K,D>>< / code>< / p>

< p>是我想要以某种方式可能(没有策略模式解决方法)?

meth 调用者指定了h2_lin>解决方案

C $ C>;你不能强迫它是 meth 中的其他特殊类型。如果按 D 您的意思是 List< List<>> ,那么您应该写:

  List< Map< K,List< List< Byte>>>> daBytes = new ArrayList< Map< K,List< List< Byte>>>>(); 

如果您的意思是 D 是任意的,那么你应该写

 列表< Map< K,D>> daBytes = new ArrayList< Map< K,D>>(); 


I would expect in the code below :

public static <K, D extends List<T>, T> List<Map<K, D>> meth(K k, D d) {
    List<Map<K, D>> daBytes = (List<Map<K, D>>)
            new ArrayList<Map<K, List<List<Byte>>>>();
    // ...
}

the compiler to figure out that D is a List<List<Byte>> and/or that T is a <List<Byte> - and to actually get rid of the cast. Instead I get :

Cannot cast from ArrayList<Map<K,List<List<Byte>>>> to List<Map<K,D>>

and I need the cast anyway.
Why ? Is what I want somehow possible (without the strategy pattern workaround) ?

解决方案

D is specified by the caller of meth; you can't force it to be some other particular type inside meth. If by D you mean List<List<Byte>>, then you should write that:

List<Map<K, List<List<Byte>>>> daBytes = new ArrayList<Map<K, List<List<Byte>>>>();

If you mean for D to be arbitrary, then you should be writing

List<Map<K, D>> daBytes = new ArrayList<Map<K, D>>();

这篇关于如何将通用参数绑定到特定的List子类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆