Make a Map and take the values

Asked

Viewed 259 times

1

I have a Map and within it I have another Map and I need to make a for and take the values that are in those maps, how it can be done?

The structure and shape that the Map is created like this:

Map<String, Map<String, String>> mapValues = new HashMap<>();

mapValues.put("laranja", Map.of("lucas", "carro"));
mapValues.put("maça", Map.of("marcos", "moto"));
mapValues.put("morango", Map.of("eduardo", "carro"));

The value laranja corresponds to lucas and carro, so on and so forth.

Trying

for(Map.Entry<String, Map<String, String>> entry : mapValues.entrySet()){
    String key = entry.getKey();
    Map<String, String> value = entry.getValue();
    System.out.println(String.format("key: %s | value: %s", key, value));
}

I made an attempt that way, but from what I saw in the moment I was thrashing is that I’ll only get the value of the first map.

Way the die will be displayed: the file is a JSP so I will present it like this:

<div>
  <p><%ingrediente%></p>
  <p><%nome%></p>
  <p><%formaDeEntrega%></p>
</div>
  • I could post how I’d like to present the data?

  • @Danizavtz edited the post and put in which way I will display the values

1 answer

2


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()));
        }
    }
}

Browser other questions tagged

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