Collection Framework
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.Collection Framework
Collection framework was not part of original Java release. Collections were added to J2SE 1.2. Prior to Java 2, Java provided ad-hoc classes such as Dictionary, Vector, Stack and Properties to store and manipulate groups of objects.
Collection framework provides many important classes and interfaces to collect and organize a group of like objects.
Important Interfaces of Collection API
| Interface | Description |
|---|---|
| Collection | Enables you to work with groups of the object; it is at the top of the collection hierarchy |
| Deque | Extends queue to handle double-ended queue. |
| List | Extends collection to handle sequences list of object. |
| Queue | Extends collection to handle special kind of list in which elements are removed only from the head. |
| Set | Extends collection to handle sets, which must contain the unique element. |
| SortedSet | Extends sets to handle sorted set. |
Collection Hierarchy
All these Interfaces give several methods which are defined by collections classes which implement these interfaces.
Why Collections were made Generic?
Generics added type safety to Collection framework. Earlier collections stored Object class references. Which means any collection could store any type of object. Hence there were chances of storing incompatible types in a collection, which could result in runtime mismatch. Hence Generics was introduced, now you can explicitly state the type of object being stored.
Collections and Autoboxing
We have studied that Autoboxing converts primitive types into Wrapper class Objects. As collections don't store primitive data types(stores only reference), hence Autoboxing facilitates the storing of primitive data types in a collection by boxing it into its wrapper type.
Most Commonly thrown Exceptions in Collection 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 a null object in Collection |
| IllegalArgumentException | threw if an invalid argument is used |
| IllegalStateException | threw if you try to add an element to an already full Collection |


Comments
Post a Comment