Java: Collection and Map
What is Collection, Map?
In Java, “collection” is basically a datatype that's list-like. In other languages called {list, array, vector, …}.
In Java, “map” is basically a datatype that's a set of key/value pairs. In other languages called {hash table, dictionary, map, associative list}.
In Java, the collection and map are actually Java Interfaces.
Note: “collection” and “map” are different interfaces, but sometimes the word “collection” informally mean both.
What is Interface?
A Java Interface defines a list of methods. Then, any Class can declare to implement that interface.
If a class C implements interface X and Y and Z, then it effectively means the C has all methods declared in X, Y, Z.
- A class can “implement” many interfaces.
- A interface can be “implemented” by many classes.
Java Interface can be inherited. That is, a interface Y can “extend” another interface X. So that, two interfaces may have a parent-child relationship. The parent interface is called “superinterface”. The child interface is called “subinterface”.
When a interface extends another, it basically modifies/adds more methods.
- Collection is a interface.
- Map is a interface.
- Iterable is a interface.
- Collection extends Iterable. (Collection is a child/subinterface of Iterable.)
The full names are:
java.util.Collection
java.util.Map
If you want to know how to actually write code to define interface, or for a class to implement interface, see: Java: Interface .
Sample methods of Collection Interface
The collection interface has methods like the following.
add
clear
contains
equals
isEmpty
iterator
Sample Methods of Map Interface
A map interface has methods similar to the collection interface, but also has methods that get value of a given key. Remeber, a “map” is basically a list of key/value pairs.
Some sample methods of map interface:
get(key)
keySet()
values()
put(key, value)
containsKey(value)
containsValue(value)
Children of Collection Interface
The Collection interface has the following children. Each of these interface is more specialized list-like type. (don't forget, these are all interfaces.)
- java.util.Set
- java.util.SortedSet
- java.util.NavigableSet
- java.util.Queue
- java.util.concurrent.BlockingQueue
- java.util.concurrent.TransferQueue
- java.util.Deque
- java.util.concurrent.BlockingDeque
Children of Map Interface
The Map interface has the following children:
- java.util.SortedMap
- java.util.NavigableMap
- java.util.concurrent.ConcurrentMap
- java.util.concurrent.ConcurrentNavigableMap
Implementations of Collection Interface
The following are classes that implements the collection interface.
- AbstractCollection
- AbstractList
- AbstractQueue
- AbstractSequentialList
- AbstractSet
- ArrayBlockingQueue
- ArrayDeque
- ArrayList
- AttributeList
- BeanContextServicesSupport
- BeanContextSupport
- ConcurrentHashMap.KeySetView
- ConcurrentLinkedDeque
- ConcurrentLinkedQueue
- ConcurrentSkipListSet
- CopyOnWriteArrayList
- CopyOnWriteArraySet
- DelayQueue
- EnumSet
- HashSet
- JobStateReasons
- LinkedBlockingDeque
- LinkedBlockingQueue
- LinkedHashSet
- LinkedList
- LinkedTransferQueue
- PriorityBlockingQueue
- PriorityQueue
- RoleList
- RoleUnresolvedList
- Stack
- SynchronousQueue
- TreeSet
- Vector
Implementations of Map Interface
- AbstractMap
- Attributes
- AuthProvider
- ConcurrentHashMap
- ConcurrentSkipListMap
- EnumMap
- HashMap
- Hashtable
- IdentityHashMap
- LinkedHashMap
- PrinterStateReasons
- Properties
- Provider
- RenderingHints
- SimpleBindings
- TabularDataSupport
- TreeMap
- UIDefaults
- WeakHashMap