wood burning stoves 2.0*
The moose likes Java in General and the fly likes Set & List interface extend Collection, so Why doesn't Map interface extend Collection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Set & List interface extend Collection, so Why doesn Watch "Set & List interface extend Collection, so Why doesn New topic
Author

Set & List interface extend Collection, so Why doesn't Map interface extend Collection?

UdayK Kumar
Greenhorn

Joined: Aug 05, 2009
Posts: 26
Hi,

Set & List interface extend Collection, so Why doesn't Map interface extend Collection?

I m not able to understand Sun FAQ on this topic, Can any one please explain me, in detail.


Thanks,
Uday.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4339
    
    7

They're too conceptually different. Sets and Lists are collections of single objects. A Map is a collection of pairs of objects. For instance, Collection has an add method taking a single argument. That doesn't make sense for a Map.
UdayK Kumar
Greenhorn

Joined: Aug 05, 2009
Posts: 26
Matthew Brown wrote:They're too conceptually different. Sets and Lists are collections of single objects. A Map is a collection of pairs of objects. For instance, Collection has an add method taking a single argument. That doesn't make sense for a Map.



I too agree, but, let us imagine, List contains objects which are identified by indices. don't we think as Key -pair for the list. In the same way, every object in the Set is identified by some key.. so can't we think as key-value pair.

This is my small understanding. Please help me on this elaborately...

Thanks
uday
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4339
    
    7

No, in a Set there's no key. It's just a collection of objects. You'll notice there's no method to pick a single obect from a Set, all you can do is iterate over it.

You could implement a List as if it was a Map, with integer keys, and some additional logic to enforce the constraint that the keys have to be sequential starting at zero. But I don't think you'd gain anything from it.

Maps and Collections are just too different to make it worth while trying to use the same interface. It wouldn't be any advantage Yes, you can find some ways in which they're similar, but that's not enough.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18505
    
  40

UdayK Kumar wrote:
I too agree, but, let us imagine, List contains objects which are identified by indices. don't we think as Key -pair for the list. In the same way, every object in the Set is identified by some key.. so can't we think as key-value pair.


There are actually some programming languages that treats everything like a hash -- where an array is just a key value pair, of the index and the values. The designers of Java decided not to do that; maybe its because it's roots are from C/C++ (and arrays are meant to be fast); maybe it's because they didn't think of it. Regardless of the reason, unless we have one of the Java designers here in this forum, the answer to "why" can't really be answered -- if you believe that arrays and lists were implemented wrong in the first place.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3574
    
  14

It wouldn't be very useful, in my opinion.

Here's how the class could look like:

This map is a collection of pairs. Now, what functionality does it add? Nothing more convenient than what's already declared in the Map interface.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Why does Map not extend Collection?

It is a design choice that the implementers of the standard Java API chose. You could see a Map as a collection of key-value pairs, and in the collections libraries of other programming languages it indeed works exactly that way.

So, there is no hard technical reason for why a Map is not a Collection. It could have been, but the designers just made a different choice, and it's hard to know after the fact why they made this choice.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Set & List interface extend Collection, so Why doesn't Map interface extend Collection?
 
Similar Threads
Why java.util.Set is not Serializable?
collection interface
Collection hierarchy
Why Map doesn't extend collection interface while List and Set can extend Collection interface
Map Interface