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 4611646
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:17:45+00:00 2026-05-22T01:17:45+00:00

I have a HashMap : private HashMap<TypeKey, TypeValue> example = new HashMap<TypeKey, TypeValue>(); Now

  • 0

I have a HashMap:

private HashMap<TypeKey, TypeValue> example = new HashMap<TypeKey, TypeValue>();

Now I would like to run through all the values and print them.

I wrote this:

for (TypeValue name : this.example.keySet()) {
    System.out.println(name);
}

It doesn’t seem to work.

What is the problem?

EDIT:
Another question: Is this collection zero based? I mean if it has 1 key and value will the size be 0 or 1?

  • 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-22T01:17:46+00:00Added an answer on May 22, 2026 at 1:17 am

    keySet() only returns a set of keys from your hash map, you should iterate this key set and the get the value from the hash map using these keys.

    In your example, the type of the hash map’s key is TypeKey, but you specified TypeValue in your generic for-loop, so it cannot be compiled. You should change it to:

    for (TypeKey name: example.keySet()) {
        String key = name.toString();
        String value = example.get(name).toString();
        System.out.println(key + " " + value);
    }
    

    Update for Java8:

    example.forEach((key, value) -> System.out.println(key + " " + value));
    

    If you don’t require to print key value and just need the hash map value, you can use others’ suggestions.

    Another question: Is this collection is zero base? I mean if it has 1 key and value will it size be 0 or 1?

    The collection returned from keySet() is a Set. You cannot get the value from a set using an index, so it is not a question of whether it is zero-based or one-based. If your hash map has one key, the keySet() returned will have one entry inside, and its size will be 1.

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

Sidebar

Related Questions

I have the following code private Map<KEY, Object> values = new HashMap<KEY, Object>(); public
I have a final non-static member: private final HashMap<String,String> myMap; I would like to
OK so I have this HashMap private Map<String, Player> players = new HashMap<String, Player>();
OK so this is a BIT different. I have a new HashMap private Map<String,
I have my HashMap, private final HashMap<Integer, Poll> pollmap = new HashMap<Integer, Poll>(); and
I have a HashMap: private HashMap<String, Integer> cardNumberAndCode_ = new HashMap<String, Integer>(); And later
I have HashMap 1, which contains 5 keys, all of which have Hashmaps as
I have a HashMap with millions of entries. Need to retrieve all entries whose
I have a HashMap that I'm using in Processing and I'd like to increment
i have a hashmap where every key has many values(stored in a arraylist). How

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.