• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Map Interface

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jesper : Thanks for the heads up on entrySet().

Jim ... ...
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This thread shows the advantage of a forum; you get several different useful pieces of information from different people.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic