Collection Framework:-
Collection Framework provides an architecture to store and manipulate the group of objects. All the
operations that you perform on a data such as searching, sorting, insertion, deletion etc. can be
performed by Java Collection Framework. Collection simply means a single unit of objects.
Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes
(ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).
Collection :
Collection represents a single unit of objects.
Framework:
• provides readymade architecture.
• represents set of classes and interface.
• is optional.
Collection framework
Collection framework represents a unified architecture for s. It has:
• Interfaces and its implementations i.e. classes
• Algorithm
Collections:
Array:
An array is collection of elements
which are stared in continuous
memory locations.
Limitations of array (or) disadvantages of array:
1. The size of an array is fixed; it can’t be increased
or decreased. Some times. The memory may be wasted or memory may not be
sufficient.
2. To perform the operations like Insertion, Deletion,
Searching, Sorting etc. the program has to write there own logic.
3. Because of the problems in an array the java people or
java s/w people java people have come it collection framework introduction java
1.2 versions.
Collection object:
An object is
said to be collection object if it holds or stores a group of other objects.
Collection class:
A collection class is a class whose
object can store group of other objects.
Ex:
The objects performed by s1,s2,s3 and s4 are stored multiple
times there by wasting the memory with in the JVM.
To save the memory with in the jvm when the objects
are stored in the collection object, the jvm stores the references of the
objects with in the collection objects inside of storing the objects directly.
The collections are designed to store
only objects that is the collections can not store primitive type values.
All the collection classes are
available in “java.util” (utility) package.
All the collection interfaces and
collection class and together as collection frame work.
All the collection classes are
categorized into three groups’ i.e.
1.
List: this category is used to store
group of individual elements where the elements can be duplicated. List is an
Interface. “Array list, Linked list and vector” are the implementations class
of list interfaces.
2.
Set: this category is used to store a group of individual
elements. But they elements can’t be duplicated. Set is an interface.
Hash set, Linked Hash set and Tree
set implementations of set interface.
3.
Map: this category is used to store the element in the form
key value pairs where the keys can’t be duplicated, values can be duplicated.
Map is an interfaces “Hash Map,
Linked Hash Map, Tree Map and Hash table are the implementation class of Map
interface.
}
Array List:
this class is an implementation class of list interface. This class is similar
to an Array but its size can be increased or decreased. This class is used to
store individual elements which can be duplicated. This class is “not
synchronized”.
If an object is synchronized then
only one thread can use the object at the same time, if an object is a not
synchronized then multiple threads can access the object at the same time,
which may lead to data inconsistency problems
.
.
Creation of Array List:
Syntax: Array List<E> al = new
Array List<E>();
Array List<E> al = new Array
List <E>(int initialcapacity);
Here, E represents element data type
Methods of Array List:
1.
Boolean add(Element obj): this method is used to place the
specified element to the end of List.
2.
Void add(int position, Element obj): this method is used to insert and
element at the specified position.
3.
Boolean remove(Element obj): this method is used to remove the 1st
occurrence of the specified element.
4.
Element remove(int position): this method is used to remove and
Element from the specified position.
5.
Void clear(): this method remove all the elements
available in the Array List.
6.
Int Size(): this method will return the count
of the no.of elements available in the Array List.
7.
Boolean contains(element obj): this method returns true if the
specified element is available in the Array List.
8.
Element get(int position): this method is used to access an
Element that is available in the specified position.
9.
Element set(int position, Element
obj): this method is
used replace an element in the specified position with the specified element.
Import java.util.*;
class ArrayList demo{
Public static void main(string[]
args){
//creation of ArrayList
ArrayList<String> al = new
ArrayList<String>();
//adding elements to the ArrayList
al.add(“Nokia”);
al.add(“Samsung”);
al.add(“Sony”);
al.add(“Celkon”);
al.add(“HTC”);
//insert an element into ArrayList
al.add(3,”Motorla”);
//displaying the elements
System.out.println(“List:”+al);
//deleting the elements
al.remove(“HTC”);
al.remove(1);
System.out.println(“List:”+al);
//displaying the size
System.out.println(“size:”+al.size());
//displaying the elements using
iterator
Iterator it = al.iterator();
while(it.hasNext()){
System.out.println(it.next());
}//end of while
}//end of main
}//end of class
Note: Iterator to using the display
of elements one by one. (iterator is a interface)
Difference between Array List and Linked List:
Array List is an implement of class
which follows Array structure. Array List is faster in Accessing the elements
and solver in insertion and Deletion.
Linked List is an implementation
class of List interface which follows tree structure. Linked List is solver in
Accessing the elements and faster in insertions and deletion.
Difference between Array List and Vector:
The vector class is exactly similar
to Array List. But it is synchronized.
Vector class:
This class is similar to Array List
which can store group of individual elements. It allows storing duplicate
values. Vector is a synchronized class.
Creation of vector:
Vector<E> v = new
Vector<E>();
Vector<E> v = new
vector<E>(int initial capacity);
Methods of vector:
1.
boolean
add(Element obj)
2.
void
add(int position, Element obj)
3.
boolean
remove(Element obj)
4.
Element
remove(int position)
5.
void
clear()
6.
int
size()
7.
boolean
contains(Element obj)
8.
Element
get(int position)
9.
Element
get(int position, Elemnet obj)
//Vector program
Import java.util.*;
class vectordemo{
public static void main(String[]
xyz){
//creation of vector
Vector<Integer> v = new
vector<Integer>();
//adding elements to vector
v.add (new Integer(11));
v.add(new Integer(22));
v.add(new Integer(33));
v.add(44);//auto boxing
v.add(1,99);
v.add(100);
//displaying the elements
System.out.println(“List:”+v);
//deleting the elements
v.remove(new Integer(22));
v.remove(1);
//displaying the elements using
general for loop
System.out.print(“List using for
loop:”);
for(int i=0;i<v.size();i++){
System.out.print(v.get(i)+ “ “);
}//displaying the elements using for
each loop
System.out.print(“\n List using for
each loop:”);
for(int i:v)//unboxing{
System.out.print(i+” “);
}
ListIterator lit = v.listIterator();
//displaying the elements in forward
direction
System.out.print(“\n forward
direction:”);
while(lit.hasNext()){
System.out.print(lit.next()+ “ “);
}//displaying the element in backward
direction
System.out.print(“\n backward
direction:”);
while(lit.hasPrevious()){
System.out.println(lit.previous()+”
“);
}//end of while
}//end of while
}//end of class
Boxing: It is the process of converting a primitive type to object
type and this process will be down automatically.
Unboxing: It is the process of converting an object type to primitive
type and this process will be down automatically.
Note: The for each loop is design to work with Arrays and collections only,
It is not a general purpose loop.
*Difference
between Iterator and List Iterator: Iterator and List Iterator both are
Interfaces use for accessing the elements. The Iterator can be used to access
the elements in forward direction only. Where as List Iterator accessing the
elements in both forward and reserve direction.
*Hashset
class: This class is the implementation class of set interface. It does
not Synchronized. It does not guarantee the order of insertion.
*Creation
of Hashset:
Hashset<E> hs = new
Hashset<E>();
Hashset<E> hs = new
Hashset<E>(int capacity);
*Methods
of Hashset:
1.Boolean add(Element obj): This method is used to place the
specified element into the set.
2.Boolean remove(Element obj): This method is used to delete
the specified element from the set. If it is available.
3.Boolean contains(Element obj): This method return true if
the specified element is available in the set.
4.Boolean isEmpty():This method return true if the set
isEmpty.
5.intsize(): This method returns the count of the no.of
elements available in the set.
6.void clear(): This method is used to delete all the elements
from the set.
Import java.util.*;
class Hashsetdemo{
public static void main(String[]
args){
HashSet<Integer> hs = new
HashSet<Integer>();
hs.add(12);
hs.add(23);
hs.add(34);
hs.add(45);
hs.add(56);
hs.add(67);
System.out.println(“set:” + hs);
Iterator it = hs Iterator();
while(it.hasNext()){
System.out.print(it.next()+” “);
}
}
}
*Linked
HashSet: This class is similar to HashSet class. But it will the
guarantee order of insertion.
*TreeSet:
This class is used to store group of individual elements, it does not allows
duplicate values, it is not synchronized. It stores the elements following the
natural order. (ascending/sorting order).
Note: The HashSet and LinkedHashSet will internally followed hashing
technique which will reduce the searching time.
Import java.util.*;
class LinkedHashSetdemo{
public static void main(String[]
args){
LinkedHashSet<Integer>lhs = new LinkedHashSet<Integer>();
lhs.add(12);
lhs.add(23);
lhs.add(34);
lhs.add(45);
lhs.add(56);
lhs.add(67);
System.out.println(“Set:”+lhs);
Iterator it = lhs.iterator();
While(it.hasNext()){
System.out.println(it.next()+” “);
}
}
}
//Treeset
import java.util.*;
class TreeSetdemo{
public static void main(String[]
args){
TreeSet<Integer> ts = new
TreeSet<Integer>();
ts.add(12);
ts.add(23);
ts.add(34);
ts.add(45);
ts.add(56);
ts.add(67);
System.out.println(“set:”+ts);
Iterator it = ts.iterator();
while(it.hasNext()){
System.out.println(it.next()+” “);
Vector
Synchronized
Hashtable
|
}
}
No comments:
Post a Comment