0% found this document useful (0 votes)
50 views21 pages

Java Collections Framework

The document discusses Java's Collections Framework, which provides interfaces and classes for storing and manipulating collections of data. It introduces the main Collection interfaces - Collection, List, Set, SortedSet, Map, and SortedMap. Each interface provides different functionality, with Collection being the most general and others like Set not allowing duplicates. Common implementation classes like HashSet, TreeMap, and ArrayList are also covered.

Uploaded by

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

Java Collections Framework

The document discusses Java's Collections Framework, which provides interfaces and classes for storing and manipulating collections of data. It introduces the main Collection interfaces - Collection, List, Set, SortedSet, Map, and SortedMap. Each interface provides different functionality, with Collection being the most general and others like Set not allowing duplicates. Common implementation classes like HashSet, TreeMap, and ArrayList are also covered.

Uploaded by

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

Java

Collections
Framework

Presented by,
Pronoy Debdas
University Roll Number-21301213059
University Registration Number-132131010060 of 2013
2014
Bca(H)
The Heritage Academy

What Is Collections
Framework?
First introduced with
the Java 2 platform,
Standard Edition,
version 1.2.

For storing and


manipulating groups
of data as a single
unit, a collection.

A convenient API to
many of the abstract
data types familiar
from computer
science data
structure curriculum

Maps, sets, lists,


trees, arrays,
hashtables, and other
collections.

Mathematical
Background
A collection is the same as the
intuitive, mathematical concept
of a set

Historical Collections

Array
Vector
Enumertaion Interface
Dictionary
-Hashtables
-Properties

Collection Interfaces and


Classes

Interfaces
A special
Collection that
cannot contain
duplicates.

Generalisation

Root interface for operations


common to all types of collections

Stores mappings from


keys to values

Collection

Set

Map

List

SortedSet
Stores a sequence of elements,
allowing indexing methods
Special Set that retains
ordering of elements.

SortedMap
Special map in
which keys are
ordered

Specialisation

Collection Interface
Works with a
group of
elements in as
general a
manner as
possible.

Supports basic
operations like
adding and
removing.

When you try


to remove an
element, only
a single
instance of the
element in the
collection is
removed, if
present.

Public methods of Collection in


Unified Modelling Language
(UML) notation:
boolean add(Object element)
boolean remove(Object
element)
The Collection interface also
supports query operations:
int size()
boolean isEmpty()
boolean contains(Object
element)
Iterator iterator()

Set Interface
Extends the
Collection
interface.

Forbids
duplicates
within the
collection.

Set
implementatio
n classes rely
on the
equals()
method of the
object added
to check for
equality.

HashSet and TreeSet Classes :

HashSet
needs to
implement
the
hashCode()

Elements
added to a
TreeSet
must be
sortable

Both
HashSet and
TreeSet
implement
the
Cloneable
interface.

SortedSet Interface
Provides a
special Set
interface for
maintaining
elements in a
sorted order.

The interface
provides
access
methods to
the ends of
the set as well
as to subsets
of the set.

The elements
added to a
SortedSet
must either
implement
Comparable or
you must
provide a
Comparator to
the
constructor of
its
implementatio
n class:
TreeSet.

List Interface
Extends the
Collection
interface to
define an
ordered
collection,
permitting
duplicates.

The interface
adds positionoriented
operations, as
well as the
ability to work
with just a
part of the
list.

The positionoriented
operations
include the
ability to
insert an
element or
Collection, get
an element, as
well as remove
or change an
element.

Some methods of List in Unified


Modelling Language (UML) notation:
void add(int index, Object element)
boolean addAll(int index, Collection
collection)
Object get(int index)
int indexOf(Object element)
int lastIndexOf(Object element)
Object remove(int index)
Object set(int index, Object element)
The List interface also provides for
working with a subset of the collection,
as well as iterating through the entire
list in a position-friendly manner:
ListIterator listIterator()
ListIterator listIterator(int startIndex)
List subList(int fromIndex, int toIndex)

Map Interface
Not an
extension of
the Collection
interface.
Instead, the
interface
starts off its
own interface
hierarchy for
maintaining
key-value
associations.

The interface
methods can
be broken
down into
three sets of
operations:
altering,
querying, and
providing
alternative
views.

The alteration
operations
allow you to
add and
remove keyvalue pairs
from the map.
Both the key
and value can
be null.

Some methods of Map in Unified Modelling


Language (UML) notation:
Object put(Object key, Object value)
Object remove(Object key)
void putAll(Map mapping)
void clear()
The query operations allow you to check on
the contents of the map:
Object get(Object key)
boolean containsKey(Object key)
boolean containsValue(Object value)
int size()
boolean isEmpty()
The last set of methods allow you to work
with the group of keys or values as a
collection.
public Set keySet()
public Collection values()
public Set entrySet()

HashMap and TreeMap Classes :

For inserting,
deleting, and
locating
elements in a
Map, the
HashMap
offers the
best
alternative

If you need to
traverse the
keys in a
sorted order,
then TreeMap
is your better
alternative.

HashMap
requires that
the class of
key added
have a welldefined
hashCode()
implementati
on. In
TreeMap,
elements
must be
sortable.

SortedMap Interface
A special Map
interface for
maintaining
keys in a
sorted order.

Working with a
SortedMap is
just like a
SortedSet,
except the
sort is done on
the map keys.

The
implementatio
n class
provided by
the Collections
Framework is
a TreeMap..

Expansion of Some Interfaces :


<<interface>>

List<E>

<<interface>>

Collection<E>
+add(E):boolean
+remove(Object):boolean
+contains(Object):boolean
+size():int
+iterator():Iterator<E>
etc

+add(E):boolean
+remove(Object):boolean
+get(int):E
+indexOf(Object):int
+contains(Object):boolean
+size():int
+iterator():Iterator<E>
etc
<<interface>>

SortedSet<E>
<<interface>>

Set<E>
+add(E):boolean
+remove(Object):boolean
+contains(Object):boolean
+size():int
+iterator():Iterator<E>
etc

+add(E):boolean
+remove(Object):boolean
+contains(Object):boolean
+size():int
+iterator():Iterator<E>
+first():E
+last():E
etc

Conclusion
The Collections Framework provides a well-designed
set of interfaces, implementations, and algorithms for
representing and manipulating groups of elements.
Understanding all the capabilities of this framework
reduces the effort required to design and implement a
comparable set of APIs, as was necessary prior to
their introduction.

Thanks!

Any questions?

References

o) Google.com
o) Bing.com
o) wikipedia.org
o) Collections Framework Support
for o) JDK 1.1
o) java.sun.com

You might also like