Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Map Interface

 
Arvind Vinay
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ... ...
 
Arvind Vinay
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15274
38
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper : Thanks for the heads up on entrySet().

Jim ... ...
 
Campbell Ritchie
Sheriff
Posts: 48921
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread shows the advantage of a forum; you get several different useful pieces of information from different people.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic