Scientech Easy Java Collections Framework | Need & Advantages | Scientech Easy

Saturday, October 20, 2018

Java Collections Framework | Need & Advantages

Introduction

Collections Framework in Java is one of the most valuable and interesting topics of Java language. How much important it is? Without using the collection concept, you cannot develop any project. Every Java learner has to learn this topic. According to Sun Microsystem document, Among all the java concepts, Collections Framework is the most important concept for developing the project as well as to crack the interview also. because this topic is the most favorite topic for the interviewers. So let's start with the details.

Initially, Collection Framework is simply called Java.util package or Collection API. Later on, The Sun Microsystem has introduced the collections framework in Java 1.2. It was developed and designed by "Joshua Bloch". Later on, after Java 1.2, it is known as the collections framework. From Java 1.5 onwards, The Sun microsystem added some more new concepts called Generics. Due to adding Generics concepts, a lot of changes happened in the collections classes. That's why Java 1.5 onwards, people started calling it Collections and Generics.

What is a Collection in Java?

General Definition
A collection is a group of objects. In Java, these objects are called the elements of the collection. 
Real-time Example:
1. In childhood, you had a kiddy bank. In the kiddy bank, you had collected a lot of coins. This kiddy bank is called collection and the coins are nothing but objects.
2. During the school time, you put all the important books in the schoolbag before going to school. Here Schoolbag is a collection and books are objects.
3. A classroom is a collection of students. So the classroom is nothing but a collection and students are objects.
Now you can also think in your life that entire world is the collection of human, animals, and different things. The world is a collection and human, animals and different things are different objects.

Technical Definition:
Technically, A collection is a group of objects that represent multiple objects as a single unit or single entity. In Java, it is also known as container objects(container objects means it contains other objects). It contains two types of objects. 
1. Homogeneous objects: Homo means same. Homogeneous objects can be defined as Multiple objects belong to a single class are called homogeneous objects.
For example: 
Suppose I created three objects Student s1, Student s2, and Student s3 of a single class 'Student'. Since these three objects belong to the same class that's why they are called homogeneous objects.

2. Heterogeneous objects: Hetero means different. Heterogeneous objects can be defined as Different objects belong to the different class are called heterogeneous objects
For example:
Suppose I create one object Student s1 and other one object Employee e1. Here one student object and one employee object together are called a collection of heterogeneous objects.

These objects can also be further divided into two types. They are
1. Duplicate objects: Same data of two-person objects of the same class is called duplicate objects. Suppose I create two person objects Person p1 and Person p2 and they have the same data then they are called duplicate objects.
     Person p1=new Person( "abc");
     Person p2=new Person("abc");
     
2. Unique objects: Different data two-person objects within the same class is called unique objects. For example
   Person p1=new Person("abcd");
   Person p2=new Person("abcde");
Unique or duplicate object depends on its internal data.


What is the need /purpose of Collection in Java?

Or 

What are the different ways to store values in Java application by JVM?

There are total 4 ways to store values in Java application.
1. Using variable approach: 
Suppose you want to store one value in the memory then you will create one variable like int x=10;. The purpose of this x variable is to represent one int value 10. If we want to store two int values, we will create two variables like int x=10; and int y=20;. Similarly, for three values, the third variable will be required and so on. Thus, there is no problem to store until the third or fourth value. But if we want to store 5000 values then declaring 5000 variables is the worst kind of programming practice and it will not be in the readable format. Thus, 
➲ The limitation of a variable is that it can store only one value at a time. 
➲ Readability and reusability of the code will be down.
➲ JVM will take more time for the execution.
Hence to overcome this problem, we should go to the second level "Class object".

2. Using class object approach: 
Using a class object, we can store multiple "fixed" numbers of values of different types. For example, suppose we are creating a class named Employee.
    class Employee {
       int eNo;
       String eName;
   } 
If you create the object of Employee class like this 
Employee e1=new Employee(); // So can you think that how many values can store in this employee object? The answer is only two i.e eNo and eName. but if you will want to store third value, it will not possible. Therefore, we can store a fixed number of different values. To overcome this problem, we should go to the third level "Array object". 

3. Using array object approach:
Suppose we initialize Student type of array object like this
Student[] st=new Student[5000];
Java collections Framework, Collection in Java, What is collection in java?
Collections in Java

The biggest advantage of the array is that we can store a huge number of values by using a single variable st. So that the readability of the code will be improved but there are various types of problem and limitation with the array. They are:

1. We can store multiple "fixed" number of values of homogeneous data type i.e It can store only similar type of data.
Suppose if we create Employee type array object
Employee[] emp=new Employee[5000]; // it will hold only student type objects.
for example    emp[0]=new Employee(); // valid.
                         emp[1]=new Customer(); // invalid. because here we are providing the customer type object. but we can resolve this problem by using an object array.
     Object[] ob=new Object[5000];
     ob[0]=new Employee(); // valid.
     ob[1]=new Customer(); // valid.    

2. Arrays are static and fixed in length and size. We cannot change(increase or decrease) the size of the array based on our requirement once they created. Hence to use array compulsory, we should know the size in advance which may not be possible always.
3. Array concept can be implemented for some standard data structure. For example,
➲ You cannot insert elements in some sorting order using array concept because array does support any method. You will have to write the sorting code for this but in the collection, readymade method support is available for sorting using Tree set.
➲ We cannot search using array whether the particular element is present or not. For this, we will have to write the searching code using the array but in the collection, we have one readymade method called contains() method. 
Hence, to overcome all these above limitations of arrays, we need Collection. 

4. Using collection object: By using a collection object, we can store same or different data without any size limitation. Thus, Technically, we can define collection as
A collection is a container object that is used for storing multiple homogeneous and heterogeneous, duplicate and unique elements without any size limitation.

What is a Framework in Java?

A framework is a set of several classes and interfaces which provides a readymade architecture.

What is a Collection Framework in Java?

A collection Framework defines an architecture of several pre-defined classes and interfaces which can be used to handle a group of objects as a single entity.

Difference between Collection & Collections

➲ A collection is an interface which can be used to represent a group of individual objects as a single entity whereas
➲ Collections is a utility class present in java.util package to define the several utility methods like sorting, searching, shuffle, reverse, min, and max for collection objects.

Difference between Arrays & Collections

1. Arrays are fixed in size but collections are growable in nature. That is we can increase or decrease size.
2. Arrays are not recommended to use with respect to memory whereas collections are recommended to use w.r.t. memory.
3. Arrays are recommended to use with respect to performance but collections are not recommended to use with respect to performance.
4. Arrays can store only homogeneous data element(similar type of data) but collection can hold both homogeneous and heterogeneous elements.
5. Arrays do not support any method but collections support various kind of methods.
6. Arrays can both hold primitives and object types but collections can hold only objects but not primitive.

Advantages of Collection Framework

1. It reduces the development time and the burden of designers, programmers, and users.
2. Your code is easier to maintain because it provides useful data structure and interfaces which reduce programming efforts.
3. The size of the container is growable.
4. It implements high-performance of useful data structures and algorithms that increase the performance.
5. It enables the software reuse.

Limitations of Collection Framework

1. Care must be taken to use the appropriate cast operation.
2. Compile type checking is not possible.

More on Collection
1. Collection Hierarchy in Java
2. Java List Interface
Final words:
I hope that this article will help you to know about the Collections Framework In Java and need and advantages of collections. This article has covered almost all introduction topics related to collections. 

Popular