Yes, it is possible. To access the second Map
, simply get a reference to it (using the first key):
Map<String,Object> interno = new HashMap<String,Object>()
mapTESTE.put(primeiraChave, interno);
interno.put(segundaChave, object);
...
// Obtém o valor
Object object = mapTESTE.get(primeiraChave).get(segundaChave);
// Atualiza o valor
mapTESTE.get(primeiraChave).put(segundaChave, novoValor);
// Atualiza a segunda chave
Object valor = mapTESTE.get(primeiraChave).remove(segundaChave);
mapTESTE.get(primeiraChave).put(novaChave, valor);
// Atualiza a primeira chave (para todos os valores da segunda)
Map<String,Object> interno = mapTESTE.remove(primeiraChave);
mapTESTE.put(novaChave, interno);
Whether or not this is the best option depends on whether or not there is a hierarchy between the keys. As the above example showed, tinkering in the first key affects all values independent of the second key. Sometimes this is exactly what you want, but in other situations it may not be.
If the keys are independent, then it is preferable to create an object composing the two keys:
class Chaves {
String primeira;
String segunda;
// Implementar equals e hashCode (importante)
}
And use this object as your map key:
Map<Chaves,Object> mapTESTE
You can even create auxiliary maps to search for one or another key:
Map<String,Chaves> buscaPorPrimeiraChave;
Map<String,Chaves> buscaPorSegundaChave;
(Maybe there is some class in Java that works for this, but if it exists I do not remember: arrays are not an option, since their equality operation does not take into account the contents of that array - only if it is the same object )