In many procedural languages Arrays are used as variable to represent many values. Arrays are also used to store group of objects in Java programming language but there are few problems with array.
Problems with Java Array
- . Arrays are fixed in size so when the number of element is not known in advance then array is not suitable.
- Arrays can hold objects of same class only(Homogeneous objects). So we can not store object or values of different types.
- No underlying standard data structures so we cannot utilize predefined methods. It increase programming complexity.
To overcome these problems collection concept evolved. Java provide rich API of collection.
If we want to represent a group of individual objects as a single entity than we can use collection. In short Collection is group of objects. Collection framework consist of number of classes and interfaces which are used to represent group of individual objects as single entity. This is similar concept like Container and STL(Standard Template Library) in c++. Array can also be defined in terms of collection.
Array are indexed collection of fixed number of homogeneous data elements.
Interfaces in collection Framework:
Interfaces in Collection Framework are used to represent abstract behavior of different types of collection. There are about 15 interfaces contained in java.util package. The java.util.Collection is top level interface of collection Framework, means all other interfaces of Collection Framework inherit Collection interface. Let’s have a look at list of all interfaces in collection framework.
- Iterable: It creates iterator for collection and make iterating possible.
- Collection: It is for storing objects as single entity.
- List: Collection in which insertion order is preserved.
- Set: Collection in which duplicates are not allowed.
- Queue: Queue is collection used store objects before processing. Simmilar to list but insertionis only possible at start and can be accessed from the end.
- Deque:It is double ended queue.
- SortedSet: Set in which elements are inserted in some order.
- NavigableSet: It allow to navigate through elements of Set.
- Iterator:This interface allow to iterate over the elements of collection. Maps are not iterable.
- ListIterator: List iterator allow bidirectional traversing.
- Map: Map allow us to store key, value pairs. Where duplicate keys are not allowed and each key maps only one value.
- SortedMap: Map in which we can specify sorting order of element.
- ConcurrentMap: Map allow concurrent access of elemnts by multiple threads.
- NavigableMap: Sorted map that facilitate navigataion from closest matching elements.
- ConcurrentNavigableMap: Concurrent and navigable map.