Java collections framework

Collection Framework is the set of classes and interfaces provided by sun microsystem which provides a unified module for working with collection of objects.

java.util package contain classes and interfaces of collection framework. At the core of collection framework it is named java.util.Collection
This interface difines functionality common to all collection.

=> add() : is used to add an element to a collection.
syntax : public boolean add(Object o);

=> addAll() : is used to add all the elements of one collection into another.
syntax : public boolean addAll(Collection C);

=> remove() : is used to remove an element from a collection.
syntax : public boolean remove(Object o);

=> removeAll() : is used to remove all the elements of one collection from another.
syntax : public boolean removeAll(Collection c);




List : This interface extends collection and adds the functionality of indexing to it.

Methods added by list interface:-
=> add() : is used to insert an element to a list.
Syntax : public boolean add(int index, Object element);

=> addAll() : is used to insert all the elements of one collection into a list.
Syntax : public booleanaddAll(int index, Collection c);

=> remove() : is used to remove an element stored at the given position int the list.
Syntax : public boolean remove(int index);

=> get() : is used to obtain element stored at the given position in the list.
Syntax : public Object get(int index);

=> indexOf() : is used to find out index of first occurance of the given element in the list.
Syntax : public int indexOf(Object o);

=> lastIndexOf() : is used to find out the inded of last occurance of the given element in the list.
Syntax : public int lastIndexOf(Object element);

Some other methods of collection interface:


=> retainAll() : is used to remove all the elements of the invoking collection which are not path of the given collection.
Syntax : public boolean retainAll(Collection c);

Difference between remove() and retainAll().





=> contans() : is used to search an element in a collection.
Syntax : public boolean contains(Object element);

=> containAlll() : is used to search all the element of one collection into another.
Syntax : public boolean containAll(Collection c);

=> size() : used to find out number of element in a collection.
Syntax : public int size();

=> clear() : is used to clear or remove all the elements of the collection.
Syntax : public void clear();

=> iterator() : returns an itreator for traversing the element of the collection.
Syntax : public Iterator iterator();

Iterator is an interface of collection framework which provides an implements independent method of traversing the element of collection

Methods of iterator interface are:

=> hasNext() : is used to find out whether there is an element to be travers or not.
Syntax : public boolean hasNext();

=> next() : is used to obtain the reference of next element from the collection.
Syntax : public Object next();

=> remove() : is used to remove last traversed element from the collection.
Syntax : public void remove();



ListIterator

=> listIterator() : method of List interface returns a list itreator for traversing the element of a list.
Syntax : public ListIterator listIterator();
public ListIterator listIterator(int index);

=> ListIterator : it is an extension of iterator which supports by directional traversing.

Following methods are added to this interface:
=> hasPrevious() : is used to find out whether there is an element behind current index or not.
Syntax : public boolean hasPrevious();

=> previous() : is used to obtain the reference of the previous element of list.
Syntax : public Object previous();

ArrayList : this class provides the implementation of list interface through abstract List class. An array list object represents the dynamic array.

0 Comments:

Post a Comment