What is Collections Framework in Java?
In Java collection is an object that represents a group of objects (such as the classic Vector class). ACollections framework is a unified architecture for representing and manipulating collections, enabling collections to be manipulated independently of implementation details.
What is Collection Interface?
TheCollection interface is the foundation upon which the collections framework is built. It declares the core methods that all collections will have. It is the root interface of the Collections framework. A there is no direct implementation of the Collections Interface, it is implemented using its sub-interfaces like List, Set, Queue.
For example, the SortedSet class implements the Set interface which is a subinterface of the Collection Interface.
The collection interfaces are divided into two groups. The most basic interface, java.util.Collection, has the following descendants:
- 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
- java.util.SortedMap
- java.util.NavigableMap
- java.util.concurrent.ConcurrentMap
- java.util.concurrent.ConcurrentNavigableMap
Sub Interfaces of the Collections Framework
As shown above collections framework has sub interfaces that are implemented by various classes in javaList Interface
- Elements can be accessed based on their index ( zero based indexing )
- It can contain duplicate values
- In addition to the pre defined methods of the Collection framework, List defines some of its own methods
Set Interface
- Set allows us to store different elements in different set, as in mathematics, it cannot have duplicate elements.
Queue Interface
- The
Queueinterface is used when we want to store and access elements in First In, First Out(FIFO) manner
Methods of Collection
TheCollection interface includes various methods that is used to perform different operations on objects. These methods are available in all its subinterfaces.
add()- inserts the specified element to the collectionsize()- returns the size of the collectionremove()- removes the specified element from the collectioniterator()- returns an iterator to access elements of the collectionaddAll()- adds all the elements of a specified collection to the collectionremoveAll()- removes all the elements of the specified collection from the collectionclear()- removes all the elements of the collection