Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

197
Visualizações
Java Map.getOrDefault with bounded wildcard

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();
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

One possible, but still rather clunky, solution is a helper function:

static <K1, K2, V, M extends Map<K2, V>> Map<K2, V> getOrEmpty(Map<K1, M> mapOfMaps, K1 key) {
  Map<K2, V> submap = mapOfMaps.get(key);
  return submap != null ? submap : Collections.emptyMap();
}

and then call it like

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

But I would still prefer a solution without having to define an extra function.

over 4 years ago · Santiago Trujillo Relatório

0

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);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda