GeeCON Prague 2014*
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


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
Marshal

Joined: Mar 22, 2005
Posts: 42033
    
  64
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.


Ping & DNS - my free Android networking tools app
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
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: 14193
    
  20

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: 39053
    
  23
This thread shows the advantage of a forum; you get several different useful pieces of information from different people.
 
wood burning stoves
 
subject: Map Interface