Collections in java
A Collection is simply an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data.
Collections in java is a framework that provides an architecture to store and manipulate the group of objects.
Collections framework was not a part of original Java release. Collections was added to J2SE 1.2.
All the operations that you perform on a data such as searching, sorting, insertion, manipulation, deletion etc. can be performed by Java Collections.
Framework in java means hierarchy of classes and interfaces.Collections framework is contained in java.util package. It provides many important classes and interfaces to collect and organize group of alike objects.
What Is a Collections Framework?
A collections framework is a unified architecture for representing and manipulating collections.All collections frameworks contain the following:
Interfaces : These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation.
Implementations: classes .
Algorithms :These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces.
Advantages of the Java Collections Framework
Java Collections Framework provides the following benefits
Reduces programming effort.
Increases program speed and quality.
Reduces effort to learn and to use new APIs.
Hierarchy of Collection Framework
Let us see the hierarchy of collection framework.The java.util package contains all the classes and interfaces for Collection framework.
Important Interfaces of Collection API
Interfaces of collection api are as follows:
|Interface ||Description |
|Collection ||Enables you to work with groups of object; it is at the top of Collection hierarchy |
|List ||Extends Collection to handle sequences list of object. |
|Set ||Extends Collection to handle sets, which must contain unique element and order is not preserved. |
|Queue ||Extends Collection to handle special kind of list in which element are removed only from the head. |
|Deque ||Extends Queue to handle double ended queue. |
|SortedSet ||Extends Set to handle sorted set. |
Commonly thrown Exceptions in Collections Framework
|Exception Name ||Description |
|UnSupportedOperationException ||occurs if a Collection cannot be modified |
|ClassCastException ||occurs when one object is incompatible with another |
|NullPointerException ||occurs when you try to store null object in Collection |
|IllegalArgumentException ||thrown if an invalid argument is used |
|IllegalStateException ||thrown if you try to add an element to an already full Collection |
Why Collections were made Generic ?
Generics added type safety to Collection framework. Earlier collections stored Object class references which meant any collection could store any type of object. Hence there were chances of storing incompatible types in a collection, which could result in run time mismatch. Hence Generics was introduced through which you can explicitly state the type of object being stored.
Collections and Autoboxing
As collections doesn't store primitive data types(stores only refrences), hence Autoboxing facilitates the storing of primitive data types in collection by boxing it into its wrapper type.
What next ?
Interfaces of Collection Framework in java