• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is Map not a true collection?  RSS feed

 
Gallen Thomas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I haven't been able to find a definitive answer as to why Maps are not a true collection according to java.sun.com (Oracle)

If anyone can be kind of to explain to me, it would be appreciated.

Thanks,

Gallen
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collection - group of objects. To be a part of the Collections framework, a class/interface must implement/extend the Collection Interface. That specifies general methods that can be used for grouping objects like,
boolean add(Object obj)
boolean addAll(Collection c)
boolean contains(Object obj)
... and so on

but see..in maps you are not just grouping objects, instead you are mapping a key-value pair and that in turn is grouped.
So, you cannot just implement the Collection interface and override the add(Object obj) method - because that doesnt help in mapping at all. So, a Map is not a true collection.
Anyways you can always get the Collection-view or Set-view of a Map. This is the only means by which Maps are connected to the Collections Framework.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gallen Thomas wrote:Hello,

I haven't been able to find a definitive answer as to why Maps are not a true collection according to java.sun.com (Oracle)

If anyone can be kind of to explain to me, it would be appreciated.

Thanks,

Gallen

Welcome to JavaRanch!

Did you search it? Check the following link

One might think that Map would extend Collection. In mathematics, a map is just a collection of pairs. In the Collections Framework, however, the interfaces Map and Collection are distinct with no lineage in the hierarchy. The reasons for this distinction have to do with the ways that Set and Map are used in the Java technology libraries. The typical application of a Map is to provide access to values stored by keys. The set of collection operations are all there, but you work with a key-value pair, instead of an isolated element. Map is therefore designed to support the basic operations of get() and put() which are not required by Set. Moreover, there are methods that return Set views of Map objects:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!