Introduction to Java Collection Framework
A Java Collection is a group of similar type of objects. Each object in a collection is called as an element. The Java Collection Framework is a set of various interfaces, implementations and algorithms used to represent and manipulate a collection.
The Collection Hierarchy
The root of the Java collection hierarchy is the Collection interface. Java doesn’t provide any direct implementation of this interface. It provides implementation of more specific sub-interfaces like Set and List.
A Collection implementation that impalements any of the sub-interfaces of Collection interface should provide two constructors, a no-argument constructor which creates an empty collection, and a constructor with a single argument of type Collection to create a new collection with same elements as passed in the argument. Though it’s not a mandate, these are general guidelines followed by all implementation of Collection provided by Java platform library.
Any method that modifies the collection on which they operate should throw UnsupportedOperationException if the collection does not support the operation.
The Java Collection hierarchy with the most commonly used classes is shown below:
The Collection interface is the root of the hierarchy, even though it extends the Iterable interface. Iterable is not part of the Collection framework, it is present in the java.lang package and allows iteration over the Collection. The Collection framework is a part of java.util package.
We can notice from the collection hierarchy that we do not have the Map interface in the hierarchy. That is because Map interface does not extends Collection, even though it is a part of the Collection Framework
So if Map is a part of Collection Framework why doesn’t it extend Collection interface?
That’s because Collection interface is incompatible with Map interface. A collection is a group of object but a Map is a group of key-value pairs. The two interfaces have different semantics and most methods declared in the Collection interface would not fit a Map and vice versa. For example, Collection uses the add(Object) method to add a new element to the collection, while Map uses a put(key, value) method to do the same.
The AbstractCollection class
As the name suggest the AbstractCollection class is an abstract class that implements the Collection Interface. It provides a skeletal implementation of the Collection interface to reduce the efforts required to implement it. It provides implementation of some general purpose collection methods like isEmpty(), contains(Object), toArray(), clear(), addAll(Collection), removeAll(Collection), toString() etc.
The concept of condition beans enables Spring to restrict the creation of any bean depending on the evaluation of a condition. These beans get created only when a preset condition is evaluated as true