• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Looping a map

 
Agustin John Lacson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 87
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Agustin John Lacson
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20971
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10257
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20971
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic