12 Jul 2022
12 Jul 2022
Introduction
⚫ In java if we want to store multiple items of homogenous data
types we can use “Arrays”.
⚫ Arrays can hold both primitive data types and
objects. Example:
int i[]=new int[100];
Object o[]=new Object[10];
⚫ Arrays don’t have underlying data structures and
algorithms. ⚫ The Length of the arrays is fixed.
⚫ Arrays can hold duplicate elements also.
⚫ We can’t store heterogeneous elements in an array. ⚫ Inserting
element at the end of array is easy but at the middle is difficult.
⚫ After retrieving the elements from the array, in order to process
the elements we don't have any methods.
⚫ To overcome the above mentioned limitations we prefer
Collections Framework.
Arrays Vs Collections
Arrays Collections
COLLECTION HIERARCHY
Map Hierarchy
Collection Object
⚫ A collection object is an object which can store group of other
objects.
⚫ A collection object has a class called Collection class or
Container class.
⚫ All the collection classes are available in the package called
'java.util’(util stands for utility).
⚫ Group of collection classes is called a Collection Framework.
⚫ All the collection classes in java.util package are the
implementation classes of different interfaces.
⚫ In General Collection Framework consists of ‘3’
parts 1. Algorithms (Rules)
2. Interfaces
(abstract datatypes)
3. Implementations (Concrete versions of these Interfaces)
ListIterator Interface:
⚫ ListIterator is an interface that contains methods to
retrieve the elements from a collection object, both in
forward and reverse directions. It can retrieve the elements
in forward and backward direction.
⚫ It has the following important methods:
Method Description
boolean hasNext() returns true if the ListIterator has more
elements when traversing the list in forward
direction.
element next() returns the next element.
void remove() removes the list last element that was returned by
the next () or previous () methods.
boolean hasPrevious() returns true if the ListIterator has more
elements when traversing the list in reverse
direction
element previous() returns the previous element in the list.
Enumeration Interface:
⚫ This interface is useful to retrieve elements one by
one like Iterator.
⚫ It has 2 methods.
Method Description
void clear() Removes all of the elements from this collection. (Collection will be empty)
boolean contains(Object o) Returns true if this collection contains the specified element
boolean containsAll(Collection c) Returns true if this collection contains all of the
elements in the specified collection.
boolean add(Object o) Returns true if this collection changed as a result of the call.
Returns false if this collection does not
permit duplicates and already contains the
specified element.
Method Name Description
All the above mentioned methods are the most common general methods
which can be applicable for any Collection object