-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
1/22
Collections and the
Java CollectionsFramework
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
2/22
Agenda
Collection Categories
Collection Operations
The JFC Collection Hierarchy
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
3/22
Collection
Categories
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
4/22
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 sametype Heterogeneous
Allows the elements to be of different types
Some collections allow the element type tobe 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
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
5/22
Collection Categories
Collections can be categorized by how the
elements they contain are organized
Linear
Hierarchical
Graph
Nonpositional
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
6/22
Collection Category - Linear
Elements are arranged in sequence suchthat all elements except the first have a
unique predecessor and all except the lasthave a unique successor There are two ends to every linear
collection Linear collection may or may not allow null
or duplicate entries
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
7/22
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
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
8/22
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 moresuccessors (called children)
Elements called leaves have oneparent and no children.
All other elements reside in the interior
of the tree and have one parent andone or more children
It cannot contain cycle
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
9/22
Collection Category Hierarchical
Examples Tree
general hierarchical collection that does not specify howmany 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
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
10/22
Collection Category - Graph Allows cycles An element can have zero or more
predecessors and zero or moresuccessors.
The elements of a graph are calledvertices and the connection betweenthem are edges.
Example Computer network topology
May or may not allow duplicate entries Undirected Graph
Two elements that are connected byan edge are both successor andpredecessor to one another
Directed Graph The edges clearly specify which mode
is the predecessor and which thesuccessor
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
11/22
Collection Category Nonpositional
An unordered collection does not
recognize successors or predecessors
There is no notion of a position orsequencing in an unordered collection
Elements are inserted, found, and
removed based on some uniqueidentifying value
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
12/22
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
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
13/22
Collection
Operations
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
14/22
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
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
15/22
Java Collection Methods
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
16/22
Java Collection Methods (cont)
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
17/22
Java Collection
Framework
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
18/22
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 thefoundation for constructing concrete classes
A set of concrete classes based on different
underlying data structures (sometimes referred to asbacking stores) that offer different runtimecharacteristics
A set of algorithms that works with collections
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
19/22
Java Collection Framework
Introduced with Java 2 platform
Provides implementations for a number of
common collections (list, maps, sets, stacksand vectors)
It also provides mechanisms to extend these
types to create new types (classes) that arealso part of the framework
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
20/22
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 ofobjects among unrelated, and possibly independentlywritten, components The only requirement is that the collection object being passed
around is descended from Collection, the root of the collectionshierarchy
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
21/22
Java Collection
FrameworkHierarchy
-
8/14/2019 Lesson 2 - Collections and the Java Collections Framework
22/22
Java Collection Framework
Hierarchy Collection
Provides the most general way to access acollection of elements
Duplicate elements within a collection areallowed
Elements are unordered, there is no notion ofthe 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 pairs instead of single
elements SortedMap
Extends Map A Map whose elements are sorted