KBD

Keith Devens .com

Sunday, October 12, 2008 Flag waving
Men never do evil so completely and cheerfully as when they do it from religious conviction. – Blaise Pascal (attributed)
← Brandon Harper » Simple Integration of Ant and SubversionIBM developerWorks: Connect KDE applications using DCOP →

Daily link icon Thursday, July 7, 2005

Using Java 1.5's foreach with Maps

From the third edition of the Java specification, here's an example of how you use Java 1.5's foreach construct with a Map. That's:

for(Map.Entry<K, V> e : map.entrySet())
    System.out.println(e.getKey()+": "+e.getValue());

Of course, to iterate just over the values:

for(V e : map.values())
    System.out.println(e);

To do the same thing with the keys, use keySet() instead of values();

Update (7/22): For reference, here's a way to iterate over a map that doesn't assume generics or the types of the map's key:values:

public static void dumpMap(Map mp) {
    Iterator it = mp.entrySet().iterator();
    while (it.hasNext()) {
        Map.Entry pairs = (Map.Entry)it.next();
        System.out.println(pairs.getKey() + " = " + pairs.getValue());
    }
}
← Brandon Harper » Simple Integration of Ant and SubversionIBM developerWorks: Connect KDE applications using DCOP →

Comments XML gif

146.50.22.99 wrote:

Hi, Can we change the value of each entry of the Map inside the for each ? Or is it just read only ?

∴ 146.50.22.99 | 18-Nov-2005 6:49am est | #8694

Keith (http://keithdevens.com/) wrote:

What do you think? You get object references back from getKey() and getValue(). So 1. You obviously can't assign to them and modify what the map points to. 2. If there are any mutator methods on the objects you can of course call them. Java doesn't have any "const" references like C++ does.

Keith | 18-Nov-2005 9:46am est | http://keithdevens.com/ | #8695

212.118.224.34 wrote:

This has been immensely helpful time and time again. Thank you so much.

∴ 212.118.224.34 | 7-Aug-2007 6:35am est | #10236

98.15.252.31 wrote:

Stephen Coulborne made a prototype compiler that allows:

for(KeyType k, ValueType v : map) { // do something with k and v. }

∴ 98.15.252.31 | 19-Apr-2008 1:25am est | #10641

98.15.252.31 wrote:

where KeyType/ValueType are replaced by their respective data type

∴ 98.15.252.31 | 19-Apr-2008 1:25am est | #10642

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

October 2008
SunMonTueWedThuFriSat
 1234
567891011
12131415161718
19202122232425
262728293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 7 posts

Recent comments XML

I hate PHP

Elliot Anderson,

Dude!! You the​man! The reverse replacement for​array_u...

Alex Ndungu: Oct 11, 1:35am

Call a function from a string in Python

?!code:
some_object.__getattribute​__('method_name')()
?!/code

is​the s...

Patrick Corcoran: Oct 8, 3:53pm

Spider solitaire

I have won 185 games of Spider​Solitaire at the "Difficult" level.​ What is...

75.179.28.113: Oct 8, 12:42pm

Sed one-liners

Hi.

I wanted to let you know​that I wrote an article "Famous Sed​One-Lin...

Peteris Krumins: Oct 8, 3:05am

Timesheet Calculator

Hadn't seen it before now, but my​company already uses a time​tracking prog...

Keith: Oct 7, 10:44am

Girls, please don't get breast implants

Hey everyone, 

I am new to this​blog and I have enjoyed reading all​your...

Sarah.M.: Oct 6, 9:45am

Generated in about 0.194s.

(Used 8 db queries)

mobile phone