• Post Reply Bookmark Topic Watch Topic
  • New Topic

why Map is not under Collection interface?  RSS feed

 
Vinney Shanmugam
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collection has subinterfaces such as Set, List and Queue. But Map is altogether a separate Interface. Why is this difference? Any rationale behind this sort of ordering?
Then, why Map is also called as a Collection?
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maps work with key/value pairs, while the other collections work with just values. Collections have, for example, add(myValue) methods, where Maps have put(myKey,myValue) methods. The Interface Map doesn't extend the Interface Collection because it has a different interface.
 
Matt Cartwright
Ranch Hand
Posts: 152
Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Map "maps" keys to values. It allows its content to be viewed
as a set of keys, a collection of values and a set of key-value
mappings.

A List is ordered (sequence) and its elements can be accessed
by index. Duplicate elements are supported.

A Set is collection that contains no duplicate elements.

Hope that helps.
Matt

 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinney Shanmugam wrote:Then, why Map is also called as a Collection?

Because whoever is calling it a Collection is confused, and just plain wrong. This confusion is somewhat understandable, however - interfaces like Map and Collection, as well as common implementations like HashMap, ArrayList, etc, were (almost*) all introduced to Java at the same time, in between JDK 1.1 and 1.2, as parts of what was called "the Collections Framework". This is a general term that includes both Maps and Collections. A HashMap is a Map, and is part of the Collections Framework, but is not a Collection. An ArrayList is a List, and is part of the Collections Framework, and is a Collection.

* There are two classes and one interface that are part of the Collections Framework but also predate it: Vector, Hashtable, and Enumeration. But it will be best for everyone if you simply forget you ever heard of them. Also, additional related classes and interfaces were added later, after the term "Collections Framework" was largely forgotten. These classes are fine, worth knowing about, but whether or not they are officially part of the Collections Framework is a question that's probably not worth worrying about - it doesn't matter. This entire paragraph was only included out of a misguided desire for completeness, and you should forget you ever read this.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . that I ever read what?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!