Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Questions about collections

 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I keep running into confusion while trying to make since of all the different collection classes/interfaces.

First off, why doesn't the Map interface extend the Collection interface? Is it solely because a Map takes a key/value pair?

Also, What are the properties of a Collection (capital C - interface)? What I mean is a List (which extends Collection) is unique because it defines the methods that relate to an index like get(int). So what defines something that IS-A Collection that makes it unique from something that IS-A Map?

I hope that made sense.
 
Ankit Garg
Sheriff
Posts: 9512
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why doesn't the Map interface extend the Collection interface? Is it solely because a Map takes a key/value pair?


Yes. Collection represent a collection of objects while a Map represents a key/value pair.

what defines something that IS-A Collection that makes it unique from something that IS-A Map?


Again I think the answer is the same. Collection interface provides methods such as add which lets you add an element to the collection. Map provides methods such as put which takes two arguments i.e. a Key and Value...
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Ankit! I do understand that... I guess my question was more focused on the reason behind why they did it that way? For instance why didn't they just have the Map interface extend the Collection interface and then overload the add method to accept 2 objects instead of 1? I know they had a good reason, and I'm sure it's a simple one... I just keep thinking that if I can understand why it is set up the way it is then I'll also understand much better how it works (and why).

Thanks again!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic