0% found this document useful (0 votes)
17 views14 pages

12 Jul 2022

This document provides an overview of Java collections frameworks. It discusses the differences between arrays and collections, including that collections are dynamically sized while arrays are fixed. It outlines the collection hierarchy with Maps, Sets, Lists, and Queues. It also describes common collection interfaces like Collection, interfaces for iterating over collections like Iterator and ListIterator, and methods for retrieving elements from collections.

Uploaded by

sandesh ahir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views14 pages

12 Jul 2022

This document provides an overview of Java collections frameworks. It discusses the differences between arrays and collections, including that collections are dynamically sized while arrays are fixed. It outlines the collection hierarchy with Maps, Sets, Lists, and Queues. It also describes common collection interfaces like Collection, interfaces for iterating over collections like Iterator and ListIterator, and methods for retrieving elements from collections.

Uploaded by

sandesh ahir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

COLLECTION FRAMEWORK

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.

⚫ Collections size is not fixed based on our requirement,


We can increase or decrease the size of a collection. ⚫
Collections can hold both homogeneous and heterogeneous
objects

⚫ Every collection class is implemented based on some


standard data structure & algorithms so predefined
method support is available.

Arrays Vs Collections
Arrays Collections

Arrays are fixed in size Collections are growable in nature

Arrays can hold only homogeneous Collections can hold both


data types elements. homogeneous and heterogeneous
elements.
There is no underlying data structure. Every collection class is implemented
based on some standard data
structure.
Arrays can hold both object and primitive Collection can hold only object types

Memory wise-> Recommended Memory wise->Not Recommended


Performance Wise---> Not Performance Wise--->
Recommended Recommended

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)

Overview of Set, List, Queue, Map


⚫ Set: A Set represents a group of elements (objects)
arranged just like an array. The set will grow dynamically
when the elements are stored into it. A set will not allow
duplicate elements.
⚫ List: Lists are like sets but allow duplicate values to be stored.

⚫ Queue: A Queue represents arrangement of elements in


FIFO (First In First Out) order. This means that an element
that is stored as a first element into the queue will be
removed first from the queue.

⚫ Map: Maps store elements in the form of key value pairs. If


the key is provided its corresponding value can be obtained.
Retrieving Elements from Collections:
⚫ Following are the ways to retrieve any element from
a collection object:
✓Using Iterator interface.
✓Using ListIterator interface.
✓Using Enumeration interface.
⚫ Iterator Interface: Iterator is an interface that contains
methods to retrieve the elements one by one from a collection
object. It retrieves elements only in forward direction. It has 3
methods:
Method Description
boolean hasNext() returns true if the iterator has more elements.
element next() returns the next element in the iterator.
void remove() removes the last element from the collection
returned by the iterator.

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

boolean hasMoreElements() This method tests Enumeration has


any more elements.

element nextElement() This returns the next element that


is available in Enumeration.

Understanding Collection Interface ⚫ The


root interface in the collection hierarchy. ⚫ A collection
represents a group of objects, known as its elements.
⚫ Some collections allow duplicate elements and others do
not.

⚫ Some are ordered and others unordered.


⚫ The JDK does not provide any direct implementations of
this interface.

Important Collection Interface methods


Method Name Description
int size() Returns the number of elements in this collection

boolean isEmpty() Returns true if this collection contains no elements


int hashCode() Returns the hash code value for this collection.

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

boolean addAll(Collection c) Adds all of the elements in the specified


collection to this collection

boolean remove(Object o) true if an element was removed as a result of


this call.
boolean removeAll(Object c) Removes all of this collection's elements that
are also contained in the specified collection

boolean retainAll(Collection c) Retains only the elements in this collection


that are contained in the specified collection

All the above mentioned methods are the most common general methods
which can be applicable for any Collection object

You might also like