Java Collections Framework
The Java platform includes Collections Framework. A collection is an object that represents a group of objects. Collection framework is unified architecture for representing and manipulating collections , enabling collections to be manipulated independently by using implementation details. To represent a group of individual objects as a single entity we need to use of Collection framework.
- Collection is an interface and Collections is an utility class. Under Collection interface there are 9 child interfaces which again have their respected implementation classes and methods.
- Collection is an interface which can be used to represent a group of individual objects as a single entity.
- The Collection interface defines the most common methods which are available to all the child interfaces. These common methods are used to manipulate the collection object like add() , remove() , contains() etc..
- Collections is an utility class under java.util package , which defines some static methods which are applicable to the collection object.
Collections Framework Diagram
The Collection interface is the top hierarchy to the collection framework which in turn belongs to Iterable interface.
Collection interface provides core methods applicable to the child interfaces and their implementing classes for basic operations (add() , contains() , isEmpty() etc. ) on collection objects. In the newly JDK version there is an another Interface is added called “Deque” child interface to the Queue . Below table illustrates the interfaces involved in the JDK 9.
|Collection||Enables you to work with the group of objects(collection) , it is at the top of the Collections hierarchy.|
|List||Extends Collection to handle the elements sequentially ( list of objects )|
|Set||Extends Collection interface to handle the elements as set which must contain unique elements|
|SortedSet||Extends Set , handles the elements in the sorted manner|
|NavigableSet||Extends SortedSet interface , involves in the retrieval of elements in the closest match|
|Queue||Extends Collection interface , handles special type of sets in which the elements are removed from the head.|
|Deque||Extends Queue , handles double-ended queue|
Overview of Collections Framework Diagram
1.Collection ( Interface )
- Collection interface came in the 1.2 version , it is said to be the root interface to all the collection framework.
- Defines some common methods which can be accessed by any child interface and their respective implementing classes
- Collection interface does not have any direct implementing classes
2. List interface
- List interface is the child interface to the Collection , it came in the 1.2 version of JDK
- ArrayList and LinkedList are the direct implementation classes to the List interface.
- Vector and Stack are referred as Legacy classes , as they are from the old version of JDK.
3.Set ( Interface )
- Set is the child interface to Collection . It came in the version 1.2
- Set has one child interface which is SortedSet. And SortedSet has a child interface known as NavigableSet.
- NavigableSet has a TreeSet implementation class
- HastSet and LinkedHashSet are the direct implementation classes to the Set interface.
- Set interface defines some methods which are applicable only to Set interface implementation classes and child interfaces
4. Queue interface
- Queue interface is the child interface to the Collection it came the total Queue interface came in the 1.5 version.
- PriorityQueue is the direct implementation class to the Queue interface.
- BlockingQueue is the child interface to the Queue interface.
- BlockQueue has two implementation classes they are LinkedBlockingQueue and PriorityBlockingQueue.
Advantages of Collections of Framework
- Reduces programming by providing data structures and algorithms , that there is no need to write the code explicitly.
- Increases performance by providing high performance implementation classes and methods.
- Provides interoperability between unrelated API’s by establishing a common language to pass collections back and forth.