Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6940461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:45:20+00:00 2026-05-27T12:45:20+00:00

How should I iterate through a Nested Map with such declaration? Map<String, Multiset<String>> Please

  • 0

How should I iterate through a Nested Map with such declaration?

  • Map<String, Multiset<String>>

Please suggest if there are other hashmap/list that are more effective way of doing this hash population task?

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<String, Multiset<String>> typeTextCount = 
new HashMap<String, Multiset<String>>();

Multiset<String> textAndCount 
    = TreeMultiset.create();

for (int i=0; i<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);
    }
}

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:

Iterator<Entry<String, Multiset<String>>> itTTC = 
    typeTextCount.entrySet().iterator();
while (itTTC.hasNext()) {
    Map.Entry textCt = (Map.Entry)itTTC.next();
    System.out.println(textCt.getKey() + " :\t" + textCt.getValue());
    itTTC.remove();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:45:21+00:00Added an answer on May 27, 2026 at 12:45 pm

    In your code you aren’t adding your Multiset to your Map. That’s why you are not seeing any output.

    In your code I did this:

    typeTextCount.put(types[i], textAndCount);
    

    inside the loop, and then with the same iterator I can see all the outputs like this :

    type::3 :   [bar x 24, foo x 32, ness x 24]
    type::2 :   [bar x 24, foo x 32, ness x 24]
    type::4 :   [bar x 24, foo x 32, ness x 24]
    type::1 :   [bar x 24, foo x 32, ness x 24]
    

    EDIT: Complete code for reference:

    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Map.Entry;
    
    import com.google.common.collect.Multiset;
    import com.google.common.collect.TreeMultiset;
    
    public class TestIterator {
    
        private static String[] foobarness  =
                                       {
                "foo", "bar", "ness", "foo", "bar", "foo", "ness", "bar", "foo", "ness", "foo", "bar", "foo", "ness",
                "bar", "ness", "foo", "bar", "foo", "ness"
                                       };
        private static String[] types      =
                                       {
                "type::1", "type::2", "type::3", "type::4",
                                       };
        public static void main(String[] args) {
            Map<String, Multiset<String>> typeTextCount = new HashMap<String, Multiset<String>>();
    
            Multiset<String> textAndCount = TreeMultiset.create();
    
            for (int i = 0; i < 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 (("type::1".equals(types[i])) || ("type::3".equals(types[i]))) {
                    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.add(text, 1);
                    }
                }
    
                if (("type::2".equals(types[i])) || ("type::4".equals(types[i]))) {
                    for (String text : foobarness)
                        textAndCount.add(text, 1);
                }
                typeTextCount.put(types[i], textAndCount);
            }
    
            Iterator<Entry<String, Multiset<String>>> itTTC = typeTextCount.entrySet().iterator();
            while (itTTC.hasNext()) {
                Map.Entry textCt = (Map.Entry) itTTC.next();
                System.out.println(textCt.getKey() + " :\t" + textCt.getValue());
                itTTC.remove();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do i iterate through a defaultdict(list) in Python? Is there a better way
Is there an elegant solution to use common code to iterate through hash_map /
It would like to iterate through list and render partial view for each element:
I have this HashMap<String, ArrayList<Item>> , is there a way to count the total
What I'd like to do is iterate through a navigation's list items, and fade
I would like to iterate through all elements in an std::list in parallel fashion
Technically it should iterate from 0 to rangeLength outputting the user name of the
Should developers avoid using continue in C# or its equivalent in other languages to
I'm attempting to iterate through a response from a jQuery Ajax request (returned as
I'm looking for the best way to iterate through a folder and put all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.