Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Map.getOrDefault with bounded wildcard

Tags:

java

generics

Got a Map<String, ? extends Map<String, Integer>> mapOfMaps variable.

Map<String, Integer> result = mapOfMaps.get("aaa");

works, but

Map<String, Integer> result = mapOfMaps.getOrDefault("aaa",Collections.emptyMap());

says

The method getOrDefault(Object, capture#1-of ? extends Map<String,Integer>) in the type Map<String,capture#1-of ? extends Map<String,Integer>> is not applicable for the arguments (String, Map<String,Integer>)

the same goes for

Map<String, Integer> result = mapOfMaps.getOrDefault("aaa",Collections.<String,Integer>emptyMap());

or

Map<String, Integer> result = mapOfMaps.getOrDefault("aaa",(Map<String,Integer>)Collections.EMPTY_MAP);

or even

Map<String, Integer> result = mapOfMaps.getOrDefault("aaa",new HashMap<String, Integer>());

Is there a way of using the getOrDefault like that or do I have to use the clunky way ?

Map<String, Integer> result = mapOfMaps.get("aaa");
if( result == null ) {
  result = Collections.emptyMap();
}
like image 846
MBobrik Avatar asked Dec 15 '21 06:12

MBobrik


1 Answers

You can use Collections.unmodifiableMap to view your map as Map<String, Map<String, Integer>>.

Map<String, ? extends Map<String, Integer>> mapOfMaps = new HashMap<>();
Map<String, Map<String, Integer>> view = Collections.unmodifiableMap(mapOfMaps);
Map<String, Integer> map = view.getOrDefault("foo", Collections.emptyMap());

In a single line, however, it still looks ugly, since you need to specify the generic type arguments for unmodifiableMap.

Map<String, Integer> map = Collections.<String, Map<String, Integer>>
    unmodifiableMap(mapOfMaps).getOrDefault("foo", Collections.emptyMap());

Explanation

You cannot call any method that has an unbounded or extends-bounded wildcard parameter, because the exact type of the wildcard is not known at compile time.

Let's make this simpler and look at Map<String, ? extends Number>, to which you could assign either of

Map<String, ? extends Number> map = new HashMap<String, Integer>();
Map<String, ? extends Number> map = new HashMap<String, Double>();

However, when calling map.getOrDefault(Object k, V defaultValue), there is no way to determine the type for defaultValue at compile time, since the actual type may change at runtime, even for the very same assignment (not the same instance though).

// compile-time error, could require a Double or any other Number-type
Number i = map.getOrDefault("foo", (Number)Integer.MAX_VALUE);
like image 124
Izruo Avatar answered Oct 26 '22 02:10

Izruo