This week's book giveaway is in the JavaFX forum.
We're giving away four copies of Introducing JavaFX 8 Programming and have Herbert Schildt on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Map Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Introducing JavaFX 8 Programming this week in the JavaFX forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Map Interface" Watch "Map Interface" New topic
Author

Map Interface

Arvind Vinay
Greenhorn

Joined: Feb 19, 2006
Posts: 5
I understand Set & List extends the Collection interface, but why doesn't the same hold true for the Map interface? Is there any specific design constraint?
Ulf Dittmer
Rancher

Joined: Mar 22, 2005
Posts: 42958
    
  73
For example, Collection extends Iterable (so there is one particular thing to iterate over), but Map actually has 3 Collections -its entries, its keys, its values- and it's not clear which one of those should be given preference to iterate over.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44464
    
  34
Welcome to the Ranch .

A set might be {1, 2, 3} and a list might be [1, 2, 2, 1, 3, 2, 3, 1, 2, 3] (which when converted to a set is identical to {1, 2, 3}. But a Map is {"Campbell" ↦ "beginning", "Rob" ↦ "General", "Janeice" ↦ "beginning", "Maneesh" ↦ "Swing"}.

A Map keeps all its data in a different form: pairs, forming a function. So it has to handle those data differently.
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Another way to see it is to observe that the behaviors needed to manage a set
are quite different from those needed for a map. So there is no design constraint.
Set, List and Map are just separate designs that meet individual needs. It also
turns out that Set and List have some common behaviors that are removed to
the Collection interface.

Jim ... ...


BEE MBA PMP SCJP-6
Arvind Vinay
Greenhorn

Joined: Feb 19, 2006
Posts: 5
Thanks for the lucid explanation Ulf, Campbell, & Jim. That was the fastest set of replies I have ever gotten. You guys rockkkkk!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14991
    
  27

Ulf, Campbell and Jim gave good explanations, but note that it's also a question of design of the Java collections classes, and what the designers have chosen to do.

For example, there are other programming languages which also have List, Set and Map implementations, but there Map has been modeled as a collection of key-value pairs. So you can then directly iterate over a Map, and you'd iterate over the key-value pairs. In Java the designers of the collections classes decided to do it diffently; in Java you explicitly have to get the entrySet() of a map if you want to iterate over the key-value pairs.

So it's not necessarily so that Map isn't a Collection, it's just a choice the designers of the Java collections classes made.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Jesper : Thanks for the heads up on entrySet().

Jim ... ...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 44464
    
  34
This thread shows the advantage of a forum; you get several different useful pieces of information from different people.
 
 
subject: Map Interface
 
It's not a secret anymore!