Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2007

Tip: Looking for answers? Try searching our database.

Iterating over Maps: Am I missing something?

Thread view: 
dsh0105@gmail.com - 06 Feb 2007 19:43 GMT
I've found that I often need to iterator over a Map and do something
to each element of the Map. The only way I've found to do this is to
get a get a KeySet from the map, get an Iterator from the KeySet and
use that Iterator to move over the elements of the Map.  Is their a
simpler (less verbose) way of accomplishing this:

Set<String>  keys=myMap.keySet();
Iterator<String>it=keys.iterator();
while (it.hasNext())
{
String key=it.next();
String value=myMap.get(key);
/*
Do something to the value...
*/
}

This just seems like a lot of work when you compare it to what you can
do with a ArrayList (assume this is a list of Strings for example
purposes)
for (String s: myList)
{
//Do something with s
}

Any advice appreciated.
Steve W. Jackson - 06 Feb 2007 19:52 GMT
> I've found that I often need to iterator over a Map and do something
> to each element of the Map. The only way I've found to do this is to
[quoted text clipped - 22 lines]
>
> Any advice appreciated.

Iterator<String> it = map.values().iterator();

HTH.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

Patricia Shanahan - 06 Feb 2007 22:13 GMT
> I've found that I often need to iterator over a Map and do something
> to each element of the Map. The only way I've found to do this is to
[quoted text clipped - 22 lines]
>
> Any advice appreciated.

What is preventing you from using the new loop syntax with the Map?

import java.util.HashMap;
import java.util.Map;

public class MapTest {
  public static void main(String[] args) {
    Map<String,String> myMap = new HashMap<String,String>();
    myMap.put("x","a");
    myMap.put("y","b");
    System.out.println("Values only");
    for(String value: myMap.values()){
      System.out.printf("Value: %s%n",value);
    }
    System.out.println("Keys and values");
    for(Map.Entry<String,String> entry: myMap.entrySet()){
      System.out.printf("Key: %s, Value: %s%n",
          entry.getKey(),entry.getValue());
    }
  }
}

Patricia
dsh0105@gmail.com - 07 Feb 2007 03:36 GMT
> dsh0...@gmail.com wrote:
> > I've found that I often need to iterator over a Map and do something
[quoted text clipped - 48 lines]
>
> Patricia

The only think preventing me from using the new looking was a mental
block:). It's obvious now. How embarrassing. Thanks for the help.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.