2 回答

TA貢獻(xiàn)1836條經(jīng)驗 獲得超5個贊
您需要使用另一種變體Collectors.toMap
,以便傳遞合并函數(shù)和供應(yīng)商以在TreeMap
那里收集:
return?input.getInputParams() ????????.entrySet() ????????.stream() ????????.filter(params?->?params.getKey().startsWith("accepted"))?//?small?change ????????.collect(Collectors.toMap(Map.Entry::getKey,?Map.Entry::getValue, ????????????????(a,?b)?->?b,?TreeMap::new));

TA貢獻(xiàn)1877條經(jīng)驗 獲得超6個贊
最終,該方法不會編譯,因為Collectors.toMap()返回Map,而方法簽名需要返回類型為SortedMap。
我不知道誤導(dǎo)性的“靜態(tài)上下文”錯誤消息背后的原因;但是當(dāng)我嘗試使用 Gradle 構(gòu)建代碼時,我收到了一條稍微有用的消息。
error: incompatible types: inference variable R has incompatible bounds
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
^
equality constraints: Map<K,U>
lower bounds: SortedMap<String,String>,Object
Collectors.toMap()您可能需要接受 a的重載版本Supplier<Map>,以便您可以提供SortedMapfor 輸出。
添加回答
舉報