Collection interface is a member of the Java Collections framework. A collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered.
The Java collections framework provides various data structures and algorithms that can be used directly.
Java Collections is a topic often brought up on technical interviews for Java developers, if you are preparing for Java Developer interviews then you may know that Java Collection are a very important topic for Java Interviews. If you want to crack the Java interview then you must prepare for Java collections.
Collections Framework Vs. Collection Interface
People often get confused between the collections framework and Collection
Interface.
The Collection
interface is the root interface of the collections framework. The framework includes other interfaces as well: Map
and Iterator
. where as Collections are utility classes
All the methods of the Collection
interface are also present in its subinterfaces, these are
Collection hierarchy
Before understanding the different components in the above framework, let’s first understand a class and an interface.
- Class: A class is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type.
- Interface: Like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract (only method signature, nobody). Interfaces specify what a class must do and not how. It is the blueprint of the class.
What is Java Collections Framework?
Java Collections are similar to containers that consists of multiple items in a single unit. for e,g. collections of students, list of names etc. Java Collections framework provides unified architecture for manipulating and representing Collections.
Advantages of the Java Collection Framework
the following are the advantages of the collection framework.
- Consistent API: The API has a basic set of interfaces like Collection, Set, List, or Map, all the classes (ArrayList, LinkedList, Vector, etc) that implement these interfaces have some common set of methods.
- Reduces programming effort: A programmer doesn’t have to worry about the design of the Collection but rather he can focus on its best use in his program. Therefore, the basic concept of Object-oriented programming (i.e.) abstraction has been successfully implemented.
- Increases program speed and quality: Increases performance by providing high-performance implementations of useful data structures and algorithms because in this case, the programmer need not think of the best implementation of a specific data structure. He can simply use the best implementation to drastically boost the performance of his algorithm/program.
Here are the few points about collections in Java, along with a link for further reading.
Collection Interface
This is the root interfaces of the java collection classes hierarchy.A Collection represents a group of objects known as its elements.There is no direct implementation of Collection interface.It has very useful methods like size(),isEmpty(),contains(),add(),remove(),iterator() etc.
List Interface: List interface is an ordered collection meaning you can access the elements of a List in specific order. and by an index too. It can contain duplicate elements.Since List is an interface you need to provide a concrete implementation of the interface in order to use it.Common implementations include ArrayList and LinkedList. and it’s suitable for scenarios where order matters.
Set Interface: A Set is an unordered collection that doesn’t allow duplicates. Examples of Set implementations are HashSet and TreeSet. It’s useful when you need to store unique elements. Set interface stores unique elements meaning each element can only exists once in a Set
Queue Interface : A Queue interface is inherited from the Java Collections interface. The Queue is a linear Collection that offers data manipulation operations on the collection elements, and follows the FIFO(First In First Out) principle. There are various classes like PriorityQueue, ArrayDeque, etc. Since all these subclasses implement the queue, we can instantiate a queue object with any of these classes.
Map interface: Map interface represents a mapping between a key and a value. It doesn’t allow duplicate keys, and you can retrieve values by their keys. Map is an interface you need to provide a concrete implementation of the interface in order to use it. Common implementations include HashMap and TreeMap.