In Java, collection framework is one of the most important topic in core java and here in this tutorial, I explained each and everything in depth with internal implementations. This tutorial is helpful to freshers and developers and being a Java developer you should learn collection framework.
Before going to learn collection framework we have to know few points about why we are going to the collection.
In generally we declare variables and assign values to them in a normal way
int number1=10;
int number2=20;
int number3=30;
now I want to declare 1000 variables in the same way
int number1=10;
int number2=20;
int number3=30;
;
;
int number1000=10000;
This is the bad programming and it increases program length and decreases the readability of the program.
To overcome this problem we have array concept, we can assign 1000 values by using a single array.
int[] number=new int[1000];
10
|
20
|
30
|
……..
|
900
|
1000
|
0 1 2 ………… 998 999
There are few limitations for using array
Fixed in size:
Once we create array size we can not modify it i.e. we can not increase or decrease the array size.
e.g: I created an array with 1000 size to store 1000 employee details but at runtime, I want to insert 2000 employee details or just 50 employee details at this case we can not increase or decrease the array size.
Allow only homogeneous elements:
An array allows only homogeneous elements i.e. similar type elements
package com.collection;
public class Test {
public static void main(String[] args) {
Employee[] emp=new Employee[1000];
emp[0]=new Employee(101, "emp1");
emp[1]=new Employee(102, "emp2");
emp[1]=new Customer(103, "emp3");
}
}
Here array is Employee type but we tried to insert Customer type then we will get compile time error as
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from Customer to Employee
at com.collection.Test.main(Test.java:7)
we can slove this by taking Object class. Object class is super class of all java classes
Object[] emp=new Object[1000];
emp[0]=new Employee(101, "emp1");
emp[1]=new Employee(102, "emp2");
emp[1]=new Customer(103, "emp3");
No data structure
No data structure for implementing arrays and no ready-made methods not support. We have to implement everything explicitly.
To overcome this problem collection concept introduced in Java 1.2 version
Collection
The collection is root level of the interface for collection framework, the collection represents a group of multiple objects as a single entity.
=> We can increase or decrease the size based on our requirement
=> Collection allow any type of element i.e. both homogeneous and heterogeneous elements
=> Every collection class is implemented by some standard data structure.
Difference between collection and array
collection
|
array
|
Growable in size
|
Fixed in size
|
Allows any type of element i.e. heterogeneous and homogeneous
|
Allow only homogeneous data elements
|
Every collection class is implemented by some standard data structure
|
No data structure for array implementation
|
Collections allows only object types, not primitive types
|
Arrays allows both object and primitive types
|
Collections are recommended with respect to memory
|
Arrays are not recommended with respect to memory
|
Performance wise collections are not good
|
Array are recommended performance wise
|
No comments:
Post a Comment