• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Saloon Keeper
Posts: 14496
325
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic