Using Map<K,V> to access Object

Asked

Viewed 37 times

-1

I have a Key value object, where the value is an Object

Map<String, String> endereco = new Map<String, Object>();
endereco.put("logradouro", "Rua Um");
Map<String, Object> aluno = new Map<String, Object>();
aluno.put("nome","Teste Teste");
aluno.put("endereco", endereco);

To recover the name of the Student we do:

aluno.get("nome");

But to recover the backyard from the address as we do?

I tried to

aluno.get("endereco").get("logradouro");

But it doesn’t work.

1 answer

0


You have to cast, note that you created the address map as Object, so the get of this map returns an Object and not the street map. I believe that solves your problem:

((Map<String, String>) aluno.get("endereco")).get("logradouro")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.