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

  • Home
  • SEARCH
  • 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 8309065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T18:59:45+00:00 2026-06-08T18:59:45+00:00

I have a HashMap, which contains another HashMap. I want to iterate over the

  • 0

I have a HashMap, which contains another HashMap. I want to iterate over the first HashMap and use the Key values from that. Then, as I iterate over the first HashMap I want to start an inner loop iterating over the second HashMap, getting all the values.

The problem I have so far is that I can’t figure out how to get the keys from the Iterator.

HashMap<String, HashMap<Integer, String>> subitems = myHashMap.get("mainitem1");

Collection c = subitems.values();
Iterator itr = c.iterator();

while(itr.hasNext())
{
    // Get key somehow? itr.getKey() ???

    // contains the sub items
    HashMap productitem = (HashMap)itr.next();
}

The data that i get from subitems is this:

{Item1{0=sub1, 1=sub2}, Item2{0=sub3, 1=sub4}}

Then, in the while loop productitem contains the ‘sub items’. But i can’t find out where i can get the key value ‘Item1’ and ‘Item2’ from.

How can i get those?

  • 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-06-08T18:59:46+00:00Added an answer on June 8, 2026 at 6:59 pm

    You can’t get the key from values().iterator().

    You need to use entrySet().iterator(). That will return Map.Entry<K,V> objects on which you can call getKey() and getValue().

    for (Map.Entry<Integer,Key> entry : subitems.keySet()) {
      Integer key = entry.getKey();
      String value = entry.getValue();
    
      // do stuff
    }
    

    I’d also like to add that having deeply nested maps of lists of maps is usually a sign that you really want to write custom classes to hold your data. Especially when the maps have pre-defined keys to be used and interpretation of the values in the lists depends on the position within the list! I call this code smell “object denial”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a hashmap which I want to copy for other use. But whenever
I have a service running in my Android application which contains a HashMap that
I have HashMap 1, which contains 5 keys, all of which have Hashmaps as
I have an XML file which contains multiple nodes that have children. If the
I have a cache class which contains a volatile HashMap<T> to store cache items.
In class 1 I have a hashmap which I send to my CustomAdapter. map.put(year,
I have two hashmaps and I would like to fill a third hashmap which
I have a class in which i have intialized hashmap in static block. Passing
If I have the value foo , and a HashMap<String> ftw for which ftw.containsValue(foo)
I have a HashMap that I'm using in Processing and I'd like to increment

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.