Lokeshkumar R
Lokeshkumar R

Reputation: 615

Getting error on passing Collections.emptyMap as an argument

I don't understand why I am getting the error on passing the Collections.emptyMap() as an argument while assigning Collections.emptyMap() to an map reference does not provide error, below is the code sample I tried, I am using JDK1.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) {

    }
}

Upvotes: 2

Views: 1325

Answers (1)

Jacob G.
Jacob G.

Reputation: 29680

Because you're using JDK 1.7, you are not able to benefit from the improved type inference in JDK 8 and above. It would be best to update the version of Java that you're compiling with. If that's not an option, then you must explicitly pass the arguments of the Map to the function when passing Collections#emptyMap as an argument:

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

Upvotes: 5

Related Questions