Collection Tutorial – Collection Framework and Generics

Definition and Need of Collection tutorial –

Collection is a Java Object that is used to group homogeneous & heterogeneous, duplicate and unique objects without size limitation for carrying multiple objects at a time from one application to another application among multiple layers  of MVC  architecture as a method argument and return type.

Why collection class are given when we have object[] to group heterogeneous object ?

All array object has below two problems

  1. It allows us to store only same type of elements.
  2. It is fixed in size.

The first problem can be resolved in using java.lang.Ojbect[]. Using Object[] we can collect all types of objects But the second problem can’t be solved automatically. We should develop below code to solve this problem.

  • Create array object with initial size and store elements.
  • Once array size is reached its capacity, execute below steps to store new element.
    • Create another temporary array with required size.
    • Copy old array values to new array.
    • Add new element to new array at the end of all its elements
    • Assign new array object reference to old array referenced variable.

collection tutorial

All collection classes are defined in java.util package are –

 collection tutorial

Why the name collection framework?

Framework is semi-finished reusable application which provides some common low level services for solving reoccurring problems and that can be customized according to our requirement.

Example –

  • Computer is a framework, it can be used by all people according to their requirement.

Need of Collection framework classes?

In Java Collection Framework classes are used to store and transport objects are of same and different types without size limitation.

Advantage of collections framework

  • Reduce programming effort by providing useful data structure and algorithm so you don’t have to write them yourself.
  • Increase performance by providing high-performance implementations of useful data structure and algoithems.
  • Reduce the effort required to learn APIs by establishing a common language to pass collections back and forth.

Types of Collection Framework Hierarchy

The collection framework is divide into two hierarchies to store object in array format and (key, value) pair format, they are :

  1. Collection Hierarchy
  2. Map Hierarchy

Common Terminology –

collection of objects

The collection of object that contain other normal class object is called collection of objects.

collection of collections

The collection of object that contain other collection object is called collection of collection.

collection of maps

The collection of object that contain map object is called collection of maps.

Leave a Comment

%d bloggers like this: