| Author |
Looping a map
|
Agustin John Lacson
Greenhorn
Joined: Sep 18, 2012
Posts: 3
|
|
Hello there.
Im kinda new to java but I need to get something done so.. Can you help me with this question?
Given a Map declared as Map<String, Date> birthdays;
how do I loop through the map to display the person's name (key), and their corresponding birthdates?
|
 |
Aniket S. Kulkarni
Ranch Hand
Joined: Jun 15, 2011
Posts: 86
|
|
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
|
Aniket Kulkarni
Oracle Certified Professional, Java SE 6 Programmer.
|
 |
Agustin John Lacson
Greenhorn
Joined: Sep 18, 2012
Posts: 3
|
|
Ohh thanks for the tip but since I'm kinda new I don't know how to use any of those. Can you demonstrate using a pseudo code or something? just something that can show me how I can loop through given my circumstances...
thanks in advance
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
|
First you'll want to get the keyset of the Map. That's a Set which contains the keys. Then you'll want to iterate over that set and for each key, you can get the corresponding value from the Map.
|
 |
Agustin John Lacson
Greenhorn
Joined: Sep 18, 2012
Posts: 3
|
|
Ohhh I can somehow get it.. So I used Iterator and preferred using hasNext...
Is this wrong?
public boolean hasnext(){
if (Map = hasnext)
return Map;}
|
 |
java boyinredglass
Greenhorn
Joined: Jun 16, 2012
Posts: 2
|
|
Yes, it's wrong.
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.
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
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.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4761
|
|
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:
keySet()
values(), and
entrySet()
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.
Winston
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
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
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.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
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.)
|
 |
 |
|
|
subject: Looping a map
|
|
|