File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
UdayK Kumar
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20831
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Stephan van Hulst
Bartender
Pie
Posts: 4826
34
Chrome Netbeans IDE Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic