Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to iterate through Nested Map and Multiset? - Java/Guava
    text
    copied!<p>How should I iterate through a Nested Map with such declaration? </p> <ul> <li><code>Map&lt;String, Multiset&lt;String&gt;&gt;</code></li> </ul> <p>Please suggest if there are other hashmap/list that are more effective way of doing this hash population task?</p> <pre><code>import com.google.common.collect.Multiset; import com.google.common.collect.TreeMultiset; String[] foobarness = {"foo" , "bar", "ness", "foo", "bar", "foo", "ness", "bar", "foo", "ness", "foo", "bar", "foo", "ness", "bar", "ness", "foo", "bar", "foo", "ness"}; String[] types = {"type::1", "type::2", "type::3", "type::4",}; Map&lt;String, Multiset&lt;String&gt;&gt; typeTextCount = new HashMap&lt;String, Multiset&lt;String&gt;&gt;(); Multiset&lt;String&gt; textAndCount = TreeMultiset.create(); for (int i=0; i&lt;types.length; i++) { // I know it's kinda weird but in my task, // i want to keep adding only 1 to the count for each entry. // Please suggest if there is a better hashmap/list for such task. if ((types[i]== "type::1") or (types[i]== "type::3")) { for (String text : foobarness) { // I don't worry too much about how i // populate the Map, it is iterating through // the Map that I have problem with. textAndCount.put(text, 1); } } if ((types[i]== "type::2") or (types[i]== "type::4")) { for (String text : foobarness) textAndCount.put(text, 1); } } </code></pre> <p>So now the hashmap is populated, how do i iterate through that complex nested map? I've tried the code below but I only got the 1st getValue() from my Multiset: </p> <pre><code>Iterator&lt;Entry&lt;String, Multiset&lt;String&gt;&gt;&gt; itTTC = typeTextCount.entrySet().iterator(); while (itTTC.hasNext()) { Map.Entry textCt = (Map.Entry)itTTC.next(); System.out.println(textCt.getKey() + " :\t" + textCt.getValue()); itTTC.remove(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload