You are on page 1of 22

Collections and the

Java Collections
Framework
Agenda

 Collection Categories
 Collection Operations
 The JFC Collection Hierarchy
Collection
Categories
What is a collection?
 A collection is a group of elements that can
be treated as a single entity
 Types
 Homogeneous
 Requires that all of its elements be of the same
type
 Heterogeneous
 Allows the elements to be of different types
 Some collections allow the element type to
be a primitive type or an object, while some
are more restrictive
 Some collections allow duplicate elements
and others do not
 Some collections allow NULL elements and
others do not
Collection Categories
 Collections can be categorized by how the
elements they contain are organized
 Linear
 Hierarchical
 Graph
 Nonpositional
Collection Category - Linear
 Elements are arranged in sequence such
that all elements except the first have a
unique predecessor and all except the last
have a unique successor
 There are two ends to every linear
collection
 Linear collection may or may not allow null
or duplicate entries
Collection Category – Linear
Examples
 List
 maintains a notion of position
 access (insertions, deletions, and retrievals) can be done at any
position in a list
 also known as a sequence
 Stack
 all access are restricted to one end (top)
 trays stacked in a cafeteria
 Back button of a browser makes use of a stack
 Queue
 all insertion in which all insertions are done at one end (rear)
 all deletion are done at the other end (front)
 People waiting to use an ATM machine
Collection Category - Hierarchical
 Trees
 Elements of a tree are called nodes
 A nonempty tree has a special node
called root, which has no predecessors
(called parents) and zero or more
successors (called children)
 Elements called leaves have one
parent and no children.
 All other elements reside in the interior
of the tree and have one parent and
one or more children
 It cannot contain cycle
Collection Category – Hierarchical
Examples
 Tree
 general hierarchical collection that does not specify how
many children an element may have
 Binary Tree
 a binary tree is one in which a node can have no more
than two children
 Binary Search Tree
 a binary search tree is a binary tree that orders its
elements to facilitate searching
Collection Category - Graph
 Allows cycles
 An element can have zero or more
predecessors and zero or more
successors.
 The elements of a graph are called
vertices and the connection between
them are edges.
 Example
 Computer network topology
 May or may not allow duplicate entries
 Undirected Graph
 Two elements that are connected by
an edge are both successor and
predecessor to one another
 Directed Graph
 The edges clearly specify which mode
is the predecessor and which the
successor
Collection Category – Nonpositional

 An unordered collection does not


recognize successors or predecessors
 There is no notion of a position or
sequencing in an unordered collection
 Elements are inserted, found, and
removed based on some unique
identifying value
Collection Category – Nonpositional
 Collection
 A collection is the most basic collection type
 Duplicates are allowed
 Map
 A map associates an entry with some unique,
identifying value
 Example
 Student Record
 Dictionary
Collection
Operations
Collection Operations
 Adding an element
 Removing an element
 Replacing an element
 Retrieving an element
 Determining whether an element is contained in a
collection
 Determining the size of a collection
 Testing to see whether the collection is empty
 Traversal
 Equality
 Cloning
 Serialization
Java Collection Methods
Java Collection Methods (cont…)
Java Collection
Framework
What is a Java Collection
Framework?
 A collection framework is a software architecture
consisting of the following
 A hierarchy of interfaces that define various kinds of
collections and specifies how they are related
 A set of abstract classes that provide partial
implementation of the interfaces and serve as the
foundation for constructing concrete classes
 A set of concrete classes based on different
underlying data structures (sometimes referred to as
backing stores) that offer different runtime
characteristics
 A set of algorithms that works with collections
Java Collection Framework
 Introduced with Java 2 platform
 Provides implementations for a number of
common collections (list, maps, sets, stacks
and vectors)
 It also provides mechanisms to extend these
types to create new types (classes) that are
also part of the framework
Advantages of working with a
collection framework
 It promotes code reuse
 Use existing classes, rather than write from scratch
 Enables us to concentrate on other aspects of the problem to be
solved
 It increases robustness and decreases debugging effort
 Any time we can use a class that has been used extensively and
can be regard as bug free
 We make our program more robust and reducing the amount of
code we will need to debug
 It provides a mechanism for passing collections of
objects among unrelated, and possibly independently
written, components
 The only requirement is that the collection object being passed
around is descended from Collection, the root of the collections
hierarchy
Java Collection
Framework
Hierarchy
Java Collection Framework
Hierarchy
 Collection
 Provides the most general way to access a
collection of elements
 Duplicate elements within a collection are
allowed
 Elements are unordered, there is no notion of
the position of an element
 List
 Extends collection
 It allows duplicate elements and introduces an
ordering by supporting positional indexing
 Set
 Extends Collection
 An unordered collection that does not allow
duplicates
 SortedSet
 Extends Set
 A Set whose elements are sorted
 Map
 Is the root of a separate hierarchy
 Deals with <key,value> pairs instead of single
elements
 SortedMap
 Extends Map
 A Map whose elements are sorted

You might also like