簡體   English   中英

在傳遞Collections.emptyMap作為參數時出錯

[英]Getting error on passing Collections.emptyMap as an argument

我不明白為什么我收到上傳遞錯誤Collections.emptyMap()作為參數,而分配Collections.emptyMap()到地圖的參考不提供錯誤,下面是我試過的代碼示例中,我使用JDK1 0.7

public class Solution {
    public static void main(String[] args) {
        Solution sol = new Solution();
        Map<String, String> map = Collections.emptyMap(); //There is no compile time error on this.
        sol.calculateValue(Collections.emptyMap()); //Getting compile time error on this
    }

    //what is the difference in passing Collections.emptyMap() as a parameter
    public void calculateValue(Map<String, String> pMap) {

    }
}

因為您正在使用JDK 1.7,所以無法從JDK 8及更高版本中改進的類型推斷中受益。 最好更新正在使用的Java版本。 如果不是這種選擇,那么在將Collections#emptyMap作為參數傳遞時,必須將Map的參數顯式傳遞給函數:

calculateValue(Collections.<String, String>emptyMap());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM