user1692342
user1692342

Reputation: 5237

Incompatible types Guava Immutable Map to Java util map

How do I convert a nested Immutable Map to Map.

I have a function definition:

double total(Map<String, Map<String, Double>>);

Now for my test cases, I am writing in the following format:

val input = ImmutableMap.of(key1, ImmutableMap.of(key2, value2));

However I get an error Incompatible types: Immutable Map cannot be converted to

java.util.Map

However if it wasn't a nested Map I am able to do it.

Upvotes: 2

Views: 1126

Answers (1)

shmosel
shmosel

Reputation: 50726

Lombok is detecting ImmutableMap<String, ImmutableMap<String, Double>> as the type, which isn't compatible with Map<String, Map<String, Double>>, as explained at length here. To fix, just declare the type explicitly:

Map<String, Map<String, Double>> input = ImmutableMap.of(key1, ImmutableMap.of(key2, value2));

Alternatively, since your map is evidently read-only, you can also pass it with an upper-bounded wildcard, like this:

double total(Map<String, ? extends Map<String, Double>> m);

This will allow it to accept any subtype of Map as the value type.

Upvotes: 3

Related Questions