Q&A Categories

  • No categories

How many ways to iterator a map in java?

Dec

20

2013

How many ways to iterator a map in java?

In: Uncategorized Asked By: jagdish [2 Grey Star Level]
vote
Answer #1

Hello Jagdish,

you can traverse a map in java by the following ways:

1. Obtain the keyset and from that keyset get an iterator. then use the Iterator pattern to traverse all keys and get each value for each key.

2. Obtain the entryset and from that entryset get an iterator. then use the Iterator pattern to traverse all entries(values) and get each key for each entry. i.e. entry.getKey().

3. Use the enhanced for to iterate over the keyset i.e. for(String aKey: aMap.keySet()).

4. Use the enhanced for to iterate over the entryset i.e. for(String anEntry: aMap.entrySet()).

5. Use old good for i=0; i < keySet size.

6. Use old good for i=0; i < entrySet size.

Answers Answered By: aiboogie [ Grey Star Level]

Answer this Question

Enter name & email to post an Answer. You can also Login / Join us

For a better management of this site please consider Logging in / Joining us before posting a Question. It makes everithing easier for us.

Please enter the captcha code bellow.
Logged in users don't have to use captchas. Don't have an account? Join us!

 

Link Exchange:

You can add url to the best of the internet directory. Our url is added under Reference Directory
HE Blog Directory WEB LOG SHOWWEB LOG SHOW