Operations with HashMap

0

Good afternoon people, I need to do some operations using HashMap. I'm new to the area.

I have the following map

protected Map<String, Map<String, Integer>> elementos = new HashMap<>();'

The first String is a key, the value is a map that contains a key as String and an Integer value.

I need to traverse all values that are in this hashmap, that is, all internal hashMaps. So I need to do comparisons and a sort. Example:

HashMap<"texto1",HashMap<"Cachorro", 3>

HashMap<"texto2",HashMap<"Peixe", 2>

There I would make a comparison of the type, "If" dog "==" Fish "-> Frequency listing = (3-2); Finally, somehow, after this calculation sort out saying which text comparison is less. First post here. Java '

    
asked by anonymous 05.12.2014 / 18:50

1 answer

1

If this is a learning exercise (college or course) the full statement would help, the way you explained the problem is confusing. Here is some guidance:

        Map<String, Map<String, Integer>> elementos = new HashMap<String, Map<String, Integer>>();

    //percorre  a map
    for (String key : elementos.keySet()) {

        int total = 0;

        //percorre a map interna, agrupado por chave 
        for (Integer i : elementos.get(key).values()) {
            total=i+total;
        }

        System.out.println(key + total);

    }
    
08.12.2014 / 13:52