Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Looping a map

 
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?
 
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
 
Marshal
Posts: 25682
69
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;}
 
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE 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
 
Marshal
Posts: 69873
278
  • 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
Marshal
Posts: 25682
69
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.)
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic