What you are trying to do is declare the function. you should be invoking it.
Here's how you do it:
This is just one way of using iterators to solve your problem. You could also use entrySet instead of keySet to achieve this.
Agustin John Lacson wrote:Is this wrong?
Well, that doesn't look like real Java code at all.
If you want to loop over the entries (key-value pairs) of the map, then call entrySet() on the map and loop over the entries in the set.
Agustin John Lacson wrote:how do I loop through the map to display the person's name (key), and their corresponding birthdates?
Well, first: you need to read up on three methods provided by all Java Maps:
and then look at Aniket's post again.
You can use either of the first two styles he lists to do what you want, since they work for ANY Java collection (ListIterator only works for Lists; and I wouldn't use Enumeration at all - it's old-fashioned - although it is worth knowing that it exists).
For your needs, I have a slight preference for using entrySet() and a for-each loop, but that's just my opinion.
Why did you quote the Java 1.4.2 docs? They are about ten years out of date. And Enumeration is regarded as legacy code, not idela for new code.
Aniket S. Kulkarni wrote:Welcome!!! Agustin John Lacson to Coderanch.
There are 4 ways to retrieve any element from collection object:
1)Using for-each loop.
2)Using Iterator interface.
3)Using ListIterator interface.
4)Using Enumeration interface.
This links may help you:Iterator docs
Tutorial on Iterator
Winston Gutkowski wrote:For your needs, I have a slight preference for using entrySet() and a for-each loop, but that's just my opinion.
Yes, in my opinion that's better than using the keySet() and a for-each loop and going back to the Map to get the values (regardless of what I said earlier in the thread.)