Collections - Part I
Collection framework - Starting from Java 1.2
Earlier is was called java.utils package.
From Java 1.5 its called collection and generics framework.
Collection : Group of objects.
Eg:
Classroom is a group and students are objects.
Library is a group and books are objects.
In Java - Collection is a container object.
Container object:
- Object inside object and access them as unique object.
- Used for storing multiple homogeneous and heterogeneous, unique, duplicate objects without size limitation.
Why collection ?
- Ability to pass muliple values via single variable. Like variable a can hoold multiple variables.
- Collection -> Container objects -> And can be represented by single name/variable.
There are 4 ways to store value in Java:
- Using variable
- Variable can store only one value.
- If you want to store 100 values then we need to create 100 variables.
- This will result in performance degrade while reading values and execution time.
- So we go for class or array object.
- Using class object.
- Can store multiple fixed numbers of values of different type.
class Emp{
int rollno;
String name;
}
Emp e = new Emp();
e.rollno = 1;
e.name = "John";
- Using array object.
- Can store multiple fixed numbers of values of same type.
- int[] a = new int[5];
- Using collection object.
- When you dont know the size and type of data we use collections.
- Store multiple objects and values.
- Transfer multiple objects acrosss the applications.
Eg:
Imagine how a bike is manufactured. Showroom sends order to the factory.
- Factory collects the raw materials (objects) and assemble them to make one object called Bike.
- Bike is shipped in a container called "Collection"
Imagine online shopping cart which would allow the user to shop multiple objects of different type and it works based on collection object.
Model–view–controller (MVC) is a software design pattern commonly used for developing user interfaces that divide the related program logic into three interconnected elements.
-> Collect data [UI] view -> Business logic [Controller] -> Persistence app [Model] -> Database.
Above sequence is called "Input flow" and data travelling from DB to front end is called "Output flow".
Terminologies in Collection Framework:
1) Element - Objects inside collections. -> col.add("a");
2) Entry - Key, Value pair is called Entry -> col.add("a",10);
3) Homogeneous objects - Same class objects.
4) Heterogeneous objects - Diff class objects.
5) Unique objects - Two objects of same class with diff data or reference.
6) Duplicate objects - Two objects of same class with same data or reference.
7) Collection of objects - Collection that contains objects.
8) Collection of collections - Collection of collections.
9) Collection of maps - Collection of key,value pairs.
10) Capacity - Total no of objects we can store in a collection. Overall size.
11) Size - How many objects we store in a collection. Current Size.
12) Default capacity - First capacity when collection created.
13) Incremental capacity - Incremental capacity of collection for storing new objects.
14) Load factor - Measurement of when collection capacity is required. Load factor is 100%.
Problem with arrays:
1) Type problem - Can store data of same data type.
2) Size problem - Store fixed size of data.
3) String order problem - Always in the order of insertion.
4) Operation problem - Insert in the middle and other restrictions.
To address the above issues, We have collections.
Different operations on collections:
1) Adding object.
2) Counting object.
3) Searching object.
4) Retrieving object.
5) Removing object.
6) Replacing object.
7) Inserting object.
Java collection interface:
1) Set interface.
- The set interface is present in java.util package and extends the Collection interface is an unordered collection of objects in which duplicate values cannot be stored.
Interfaces in Java: - Kind of inheritance.
They define particular behaviour a class has.
Class can implement any number of interfaces.
To know about interface lets learn about ABSTRACT METHODS AND ABSTRACT CLASS:
- Abstract method:
Only declaration but no definition.
- Abstract class:
- If a class contains atleast one abstract method then its calls Abstract class So, we have use the keyword abstract in class.
- Object cannot be created of abstract class.
- So, Implementation of abstract methods will be written in dervied class.
- Concrete class - A class contains complete definition for all methods.
// Declaration but no definition
abstract class A{
abstract void display();
}
// Definition of the method in dervied class
class B extends A{
void display(){
System.out.println("Class A");
}
}
class Abstract{
public static void main(String args[]){
B b = new B();
b.display();
}
}
Image: Child class B with an abstract method.
abstract class A{
abstract void display();
}
abstract class B extends A{
void display(){
System.out.println("Class A");
}
abstract show(){
System.out.println("Class B");
}
}
class C extends B
class Abstract{
public static void main(String args[]){
C c = new C();
c.display();
c.show();
}
}
Will be continued....
Comments
Post a Comment