[問題] 寫method泛型時,遇到一個問題
public <ListType extends List<MapType>
,MapType extends Map<?, ?>>
void sort(ListType targetList) {
// 內容
}
上面這個method可以正常使用
List<Map<String, String>> targetList = new ArrayList<Map<String, String>>();
sort(targetList);
但我想把Map的key跟value的型態也拿出來,就不行了...
public <ListType extends List<MapType>
,MapType extends Map<KeyType, ValueType>
,KeyType
,ValueType>
void sort(ListType targetList) {
// 內容
}
List<Map<String, String>> targetList = new ArrayList<Map<String, String>>();
sort(targetList);
上面這樣要塞入targetList就出現錯誤...
Bound mismatch:
The generic method sort(ListType) of type ListMapSort
is not applicable for the arguments (List<Map<String,String>>).
The inferred type Map<String,String> is not a valid substitute
for the bounded parameter <MapType extends Map<KeyType,ValueType>>
因為我有需求在內容裡使用key的type,但一直卡住...
想請問需要怎麼改?
或者是我哪裡觀念有誤?
煩請指點,感恩。
--
我們全心全意的愛你 我們全心全意的愛你
親像愛自己的母親 親像愛自己的母親
不是你的土地特別香 不是你的物產特別豐富 929 吳志寧
因為你的懷抱這麼溫暖 因為你用艱苦的奶 養大了我們 全心全意愛你
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.250.72.112
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 6 篇):
java 近期熱門文章
PTT數位生活區 即時熱門文章