QA's approach 2 Java - Collections Framework & Generics Introduction


Why Collections & why is it called a Framework?

First Question: Why Collections?

Collections are used to overcome problems with variables, arrays, classes like size problem, type problem, non existence of predefined operations/methods, etc.

To get a Overview of the above discussed problems, click on this link

What is a Framework?

Its a semi-finished re-usable application, providing some low level services, which can be customized according to our requirement. 

Second Question: Why is it called a Framework?

Collection API is available in util package & its called a Framework for the reason that, java programmers were not restricted to just utilize the existing classes making them final but was allowed to expand on top of the exiting classes inheriting them.

Collections framework gives a clear example of when to use Interface & abstract class

Classification of Collection Framework:

In Collection Framework all the classes are classified into 3 category:


1.Container objects

meant for storing objects - it's an object that can store other objects

The below 4 Interfaces & their sub classes.

1.1 List

1.2 Set

1.3 Map

1.4 Queue


2.Cursor objects

meant for retrieving objects

2.1 Enumeration

2.2 Iterator

2.3 ListIterator


3.Utility objects

Class given to perform various operations on container objects.

3.1 Collections

3.2 Arrays


What is a Java Collection Framework?

A Java collection framework provides an architecture to store and manipulate a group of objects. A Java collection framework includes the following:
Interfaces
Classes
Algorithm

Let’s learn about them in detail:

Interfaces: Interface in Java refers to the abstract data types. They allow Java collections to be manipulated independently from the details of their representation. Also, they form a hierarchy in object-oriented programming languages.

Classes: Classes in Java are the implementation of the collection interface. It basically refers to the data structures that are used again and again.

Algorithm: Algorithm refers to the methods which are used to perform operations such as searching and sorting, on objects that implement collection interfaces. Algorithms are polymorphic in nature as the same method can be used to take many forms or you can say perform different implementations of the Java collection interface.

The Java collection framework provides access to prepackaged data structures as well as algorithms to manipulate data.

Definition:
Collection in java is a container object i.e., holding multiple objects either homogeneous or heterogeneous, unique & Duplicate as a single unit without size limitations with a single name.

Used for storing multiple objects as a single group & then Used for sending all the objects stored as a single group, at a time from one class method to another class method by passing it as a method parameter argument.

1. Its used to group homogeneous/heterogeneous objects without size limitations
2. Used for carrying multiple objects at a time from one application to another among muliple layers of MVC architecture as method arguments & return types.

When should we use Collection:

If we want to send multiple objects by containing them in a single object as a single parameter argument
or if we want to return multiple values from a method as a single return type value,we need to use collection.

Various ways to collect objects:

1. In collection format -->object will have no identity.
2. In (key,Value) pair format -Map-->Object will have identity.

Collection:
Its an interface to represent a group of objects as a single entity/unit.

Collections:
Its a utility class to define several utility methods for collection object.

Problems with Collection:

1. Not Type-Safe to use
2. Require type casting, while retrieving

Solution:  Generics

1. Provides Type-Safety for Collections
2. Resolves Type-Casting problem

Bounded & Unbounded types 

Class Test<T>
{
//Unbounded Type example
}


Class Test<T extends X>
{
//Bounded Type example, where X is a Class or Interface
}

So,as a Type parameter will bound/restrict the user from adding object types other than X type or its child/implementing classes.

Example:
Class Test<T extends Number>
{
//Bounded to add only objects of type Number & its child classes.
}

ArrayLists:

Data structure used: Re-sizable or grow able array

When we construct an array, we need to know its length. Once constructed, the length can never change. That is inconvenient in many practical applications. A remedy is to use the ArrayList class in the java.util package. An ArrayList object manages an array internally. When that array becomes too small or is insufficiently utilized, another internal array is automatically created, and the elements are moved into it. This process is invisible to the programmer using the array list.

The syntax for arrays and array lists is completely different. Arrays use a special syntax— the [] operator for accessing elements, the Type[] syntax for array types, and the new Type[n] syntax for constructing arrays. In contrast, array lists are classes, and you use the normal syntax for constructing instances and invoking methods.

However, unlike the classes that you have seen so far, the ArrayList class is a generic class—a class with a type parameter.

To declare an array list variable, you use the syntax for generic classes and specify the
type in angle brackets:

ArrayList<String> books;
books = new ArrayList<>();
or new ArrayList<String>()


Comments

Popular posts from this blog

QA's approach 2 Java - Understanding Static context

Technologies - Log4J - Create Log4j Configuration File - Where ? How ? What ?