Translate

Sunday, November 25, 2012

COLLECTIONS FRAME WORK-03


Collection

This can be used for representing a group of individual objects as a single entity. This interface defines the most common general methods. Which can be applicable for any collection implemented class object.



Colletion Interface methods

1)boolean add(Object obj)
2) boolean addAll(Collection c)
3) boolean remove(Object obj)
4) boolean removeAll(Collection c)
(Removes particular group of objects.)

5) boolean retainAll(Collection c)
(Removes all the elements except those present in ‘c’)

6) void clear()
(Removies all objects.)

7) boolean contains(Object obj)
(Checks object is there or not.)

8) boolean contains (Collection c)
9) boolean isEmpty()
10) int size()
11) Object [] toArray()
12) Iterator iterator()
(to retrieve the objects one by one.)
List interface

This can be used for representing a group of individual objects where insertion order is preserved and duplicate objects are allowed. By means of index we can preserve insertion order and we can differentiate duplicate objects.

List Interface Defines the following methods

1) boolean add(Object obj)
2) boolean add(int index, Object obj)
3) boolean addAll(Collection c)
4) boolean addAll(int index, Collection c)
5) boolean remove(Object obj)
6) Object remove(int index)
7) Object set(int index, Object new)
Old object. It replaces with the existing objected located at specified index with the new object. And it returns object.

8) Object get(int index)
9) int indexOf(Object obj)
10) int lastIndexOf(Object obj)
11) ListIterator listIterator()

ArrayList()

·        The underlying data Structure for ArrayList() is resizable Array or “Growable Array”.
·        Duplicate objects are allowed.
·        Insertion order is preserved.
·        Heterogeneous objects are allowed.
·        ‘null’ insertion is possible.

Constructors of Array List

ArrayList l = new ArrayList()

Creates an empty ArrayList object with default intitial capacity 10.
When ever ArrayList reaches its max capacity a new ArrayList Object will be created with new capacity.

capacity = (current capacity * 3/2) + 1


ArrayList l = new ArrayList(int initial capacity)
Creates an empty ArrayList Object with the specified initial capacity.

ArrayList l = new ArrayList(Collection c)
For inter conversion between collection objects.

Ex:

import java.util.*;
class ArrayListDemo
{
public static void main(String arg[])
{
ArrayList a = new ArrayList();
a.add("A");
a.add(new Integer(10));
a.add("A");
a.add(null);
System.out.println(a);
a.remove(2);
System.out.println(a);
a.add(2,"M");
a.add("N");
System.out.println(a);
}
}

ArrayList and vector classes implement RandomAccess interface. So that we can access any element with the same speed. Hence ArrayList is best suitable if our frequent operation is retrieval operation.

Usually the collection objects can be used for data transport purpose and hence every collection implemented class already implemented serializable and cloneable interfaces.

ArrayList is the worst choice if u want to perform insertion or deletion in the middle.

Note:- ArrayList is not recommended if the frequent operation is insertion or deletion in the middle.

To handle this requirement we should go for linked list.

LinkedList()

·        The underlying Data Structure for linked list is doubly linked list.
·        Duplicate objects are allowed.
·        Insertion order is preserved.
·        Heterogeneous objects are allowed.
·        ‘null’ insertion is possible.
·        Implements List, Queue, serializable, clonealbe Interfaces But not RandomAccess.

LinkedList is the bestchoice if our frequent operation is insertion or deletion in the middle(no shift operations are required)

LinkedList is the worst choice if our frequent operation is retrieval operation.

LinkedList class usually used for implementing stacks and Queues to provide support for this requirement, LinkedList class contains the following specific methods.

1. void addFirst(Object obj)
2. void addLast(Object obj)
3. Object removeFirst()
4. Object removeLast()
5. Object getFirst()
6. Object getLast()

Constructors

1. LinkedList l = new LinkedList()
2. LinkedList l = new LinkedList(Collection c)
For inter conversion between collection objects

Ex:

import java.util.*;
class LinkedListDemo
{
public static void main(String arg[])
{
LinkedList l = new LinkedList();
l.add("raju");
l.add(new Integer(10));
l.add(null);
l.add("raju");
l.set(0, "chinna");
l.add(0, "Kiran");
l.addFirst("AAAA");
l.addLast("ZZZZ");
System.out.println(l);
}
}

Inter conversion between collection objects.

ArrayList al = new ArrayList();
l1.add(10);
l1.add(20);
l1.add(30);
System.out.println("l1--->"+l1);
LinkedList l2 = new LinkedList(l1);

l2.add(1,5);
l2.add(3,5);
l2.add(5,15);
System.out.println("l2--->"+l2);
ArrayList l3 = new ArrayList(l2)
System.out.println("l3--->"+l3);



VectorClass

·        The underlying Data structure for the vector is resizable array or growable array.
·        Insertion order is preserved.
·        Duplicate objects are allowed.
·        ‘null’ insertion is possible.
·        Heterogeneous objects are allowed.
·        Best choice if the frequent operation is retrieval.
·        Worst choice if the frequent operation is insertion or deletion in the middle.
·        Vector class implemented serializable, cloneable and RandomAccess Interfaces.


Difference between vector and ArrayList?

Vector
ArrayList
1) All methods of vector are synchronized
1) no method is synchronized
2) vector object is thread safe
2) vector object is not thread safe by default
3) performance is low
3) performance is High
4) 1.0 version(Legacy)
4) 1.2 version(non Legacy)

No comments: