File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Map does not extend Iterable. Why ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Map does not extend Iterable. Why ?" Watch "Map does not extend Iterable. Why ?" New topic
Author

Map does not extend Iterable. Why ?

Graeme Byers
Ranch Hand

Joined: Apr 16, 2004
Posts: 127
Why does not Map extend Iterable ?
I notice that (old) Hashtable extends Enumeration.
Thank you.

Additional : HashMap.entrySet() allows 'casting' to a Collection and
iterating + printing each element.

[ September 14, 2008: Message edited by: Graeme Byers ]
[ September 14, 2008: Message edited by: Graeme Byers ]
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Map interface does not implement Collection interface, because it does not contain elements but contains entries of keys and their corresponding values.


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Thomas Thevis
Ranch Hand

Joined: Sep 02, 2008
Posts: 87
Why does not Map extend Iterable ?

Why should it? If you want to iterate the keys, you have the keySet() method. If you want to iterate the values, you can use the values() method. If you want to iterate the key-value-pairs, you have the already mentioned entrySet() method.


SCJP 5.0, SCJD in progress
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29249
    
139

Originally posted by Graeme Byers:
I notice that (old) Hashtable extends Enumeration.

What version of Java are you looking at? In Java 6, we have


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

for the entrySet() method, you can prefer JavaDoc.


as it returns a set view of the mappings contained in this map. and Set interface is already extends with collection.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Sun could have made Map extend Iterable, but that would require that Map itself should have an iterator() method. Imagine all the custom Map implementations that would be broken. It's bad enough they did that with the java.sql interfaces.

Besides, you can iterate over the map by using keySet(), entrySet() or values() - that's 8, 10 or 8 extra characters. Is that so much?


I have suggested that Sun implement the enhanced for loop for Maps as well:

or

Sun said that this wouldn't be implemented before Java 7 (which is logical), and by then it can most likely be implemented using closures. Therefore, don't expect to see it at all, so you'll be stuck using Map.Entry:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Graeme Byers
Ranch Hand

Joined: Apr 16, 2004
Posts: 127
Thank you Rob, that's a good reply.
I was just working through the Collection methods and it seemed rather 'missing'.
Graeme.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Map does not extend Iterable. Why ?
 
Similar Threads
equals() in subclasses
Invoking static member classes from separate class
how to check whether a word is a palindrome
"Empty" String v's String.length() = 0
Passing subclass objects to overridden methods taking superclass args.