If the maps interns have exactly a single key, you can get the entrySet
of them and take the first (and only) element:
for (Map.Entry<String, Map<String, String>> entry : mapValues.entrySet()) {
String key = entry.getKey();
// pega a chave e o valor do map interno
Entry<String, String> entrega = entry.getValue().entrySet().iterator().next();
System.out.println(String.format("ingrediente: %s | nome: %s, forma de entrega: %s",
key, entrega.getKey(), entrega.getValue()));
}
In the case, entry.getValue()
returns the map more internal, which is a Map<String, String>
. Then I get the entrySet
of this map, and iterator().next()
picks up the first element of this map (which is a Entry<String, String>
, whose key is the name and value is the form of delivery).
How "we are sure" that each map internal only has one element, I did no verification in that sense, but could be done something like:
for (Map.Entry<String, Map<String, String>> entry : mapValues.entrySet()) {
String key = entry.getKey();
Map<String, String> formasDeEntrega = entry.getValue();
if (formasDeEntrega.isEmpty()) {
System.out.println("Não há formas de entrega cadastradas para o produto " + key);
} else {
Entry<String, String> entrega = formasDeEntrega.entrySet().iterator().next();
System.out.println(String.format("ingrediente: %s | nome: %s, forma de entrega: %s",
key, entrega.getKey(), entrega.getValue()));
}
}
But I’m still assuming that there can only be at most one form of delivery.
If you can have more than one, then just make another loop us maps interns:
Map<String, Map<String, String>> mapValues = new HashMap<>();
mapValues.put("laranja", Map.of("lucas", "carro", "fulano", "ônibus"));
mapValues.put("maçã", Map.of("marcos", "moto", "ciclano", "caminhão", "zé", "a pé"));
mapValues.put("morango", Map.of("eduardo", "carro", "beltrano", "bicicleta"));
mapValues.put("uva", new HashMap<>());
for (Map.Entry<String, Map<String, String>> entry : mapValues.entrySet()) {
String key = entry.getKey();
Map<String, String> formasDeEntrega = entry.getValue();
if (formasDeEntrega.isEmpty()) {
System.out.println("Não há formas de entrega cadastradas para o produto " + key);
} else {
System.out.println("Formas de entrega para " + key);
for (Entry<String, String> entrega : formasDeEntrega.entrySet()) {
System.out.println(String.format(" - nome: %s, forma de entrega: %s",
entrega.getKey(), entrega.getValue()));
}
}
}
I could post how I’d like to present the data?
– Danizavtz
@Danizavtz edited the post and put in which way I will display the values
– Brandon Marcos