The Java Collections Framework PDF
The Java Collections Framework PDF
CSE 2011
Prof. J. Elder
-1-
Outline
Introduction to the Java Collections Framework
Iterators
Interfaces
Abstract Classes
Classes
CSE 2011
Prof. J. Elder
-2-
CSE 2011
Prof. J. Elder
-3-
CSE 2011
Prof. J. Elder
-4-
What is a Collection?
An object that groups multiple elements into a single
unit.
Sometimes called a container.
CSE 2011
Prof. J. Elder
-5-
CSE 2011
Prof. J. Elder
-6-
History
Apart from the Java Collections Framework, the bestknown examples of collections frameworks are the C++
Standard Template Library (STL) and Smalltalk's
collection hierarchy.
CSE 2011
Prof. J. Elder
-7-
Benefits
Reduces programming effort: By providing useful data structures
and algorithms, the Collections Framework frees you to concentrate
on the important parts of your program rather than on the low-level
"plumbing" required to make it work.
Increases program speed and quality: Provides highperformance, high-quality implementations of useful data structures
and algorithms.
Allows interoperability among unrelated APIs: APIs can
interoperate seamlessly, even though they were written
independently.
Reduces effort to learn and to use new APIs
Reduces effort to design new APIs
Fosters software reuse: New data structures that conform to the
standard collection interfaces are by nature reusable.
CSE 2011
Prof. J. Elder
-8-
-9-
CSE 2011
Prof. J. Elder
- 10 -
CSE 2011
Prof. J. Elder
- 11 -
Iterators
An Iterator is an object that enables you to traverse through a collection
and to remove elements from the collection selectively, if desired.
You get an Iterator for a collection by calling its iterator method.
Suppose collection is an instance of a Collection. Then to print out
each element on a separate line:
Iterator<E> it = collection.iterator();
while (it.hasNext())
System.out.println(it.next());
Note that next() does two things:
1. Returns the current element (initially the first element)
2. Steps to the next element and makes it the current element.
CSE 2011
Prof. J. Elder
- 12 -
Iterators
Iterator interface:
public interface Iterator<E> {
boolean hasNext();
E next();
void remove(); //optional
}
hasNext() returns true if the iteration has more elements
next() returns the next element in the iteration.
throws exception if iterator has already visited all elements.
- 13 -
Implementing Iterators
Could make a copy of the collection.
Good: could make copy private no other objects could change
it from under you.
Bad: construction is O(n).
CSE 2011
Prof. J. Elder
- 14 -
CSE 2011
Prof. J. Elder
- 15 -
CSE 2011
Prof. J. Elder
- 16 -
ListIterators
A ListIterator extends Iterator to treat the collection as a list, allowing
access to the integer position (index) of elements
forward and backward traversal
modification and insertion of elements.
Iterator
ListIterator
CSE 2011
Prof. J. Elder
- 17 -
ListIterators
Iterator
ListIterator
CSE 2011
Prof. J. Elder
- 18 -
CSE 2011
Prof. J. Elder
- 19 -
Levels of Abstraction
Recall that Java supports three levels of abstraction:
Interface
Java expression of an ADT
Includes method declarations with arguments of specified types, but
with empty bodies
Abstract Class
Implements only a subset of an interface.
Cannot be used to instantiate an object.
(Concrete) Classes
May extend one or more abstract classes
Must fully implement any interface it implements
Can be used to instantiate objects.
CSE 2011
Prof. J. Elder
- 20 -
Interface
Abstract Class
Collection
Class
List
Abstract
Collection
Queue
Abstract
List
Abstract
Queue
Priority
Queue
Abstract
Sequential
List
Array
List
Vector
Stack
Linked
List
CSE 2011
Prof. J. Elder
- 21 -
- 22 -
Interface
Abstract Class
Collection
Class
List
Abstract
Collection
Queue
Abstract
List
Abstract
Queue
Priority
Queue
Abstract
Sequential
List
Array
List
Vector
Stack
Linked
List
CSE 2011
Prof. J. Elder
- 23 -
- 24 -
Interface
Abstract Class
Collection
Class
List
Abstract
Collection
Queue
Abstract
List
Abstract
Queue
Priority
Queue
Abstract
Sequential
List
Array
List
Vector
Stack
Linked
List
CSE 2011
Prof. J. Elder
- 25 -
CSE 2011
Prof. J. Elder
- 26 -
Interface
Abstract Class
Collection
Class
List
Abstract
Collection
Queue
Abstract
List
Abstract
Queue
Priority
Queue
Abstract
Sequential
List
Array
List
Vector
Stack
Linked
List
CSE 2011
Prof. J. Elder
- 27 -
- 28 -
Interface
Abstract Class
Collection
Class
List
Abstract
Collection
Queue
Abstract
List
Abstract
Queue
Priority
Queue
Abstract
Sequential
List
Array
List
Vector
Stack
Linked
List
CSE 2011
Prof. J. Elder
- 29 -
CSE 2011
Prof. J. Elder
- 30 -
Interface
Abstract Class
Collection
Class
List
Abstract
Collection
Queue
Abstract
List
Abstract
Queue
Priority
Queue
Abstract
Sequential
List
Array
List
Vector
Stack
Linked
List
CSE 2011
Prof. J. Elder
- 31 -
CSE 2011
Prof. J. Elder
- 32 -
End of Lecture
Jan 17, 2012
CSE 2011
Prof. J. Elder
- 33 -
Interface
Abstract Class
Collection
Class
List
Abstract
Collection
Queue
Abstract
List
Abstract
Queue
Priority
Queue
Abstract
Sequential
List
Array
List
Vector
Stack
Linked
List
CSE 2011
Prof. J. Elder
- 34 -
CSE 2011
Prof. J. Elder
- 35 -
Interface
Abstract Class
Collection
Class
List
Abstract
Collection
Queue
Abstract
List
Abstract
Queue
Priority
Queue
Abstract
Sequential
List
Array
List
Vector
Stack
Linked
List
CSE 2011
Prof. J. Elder
- 36 -
CSE 2011
Prof. J. Elder
- 37 -
Interface
Abstract Class
Collection
Class
List
Abstract
Collection
Queue
Abstract
List
Abstract
Queue
Priority
Queue
Abstract
Sequential
List
Array
List
Vector
Stack
Linked
List
CSE 2011
Prof. J. Elder
- 38 -
- 39 -
Interface
Abstract Class
Collection
Class
List
Abstract
Collection
Queue
Abstract
List
Abstract
Queue
Priority
Queue
Abstract
Sequential
List
Array
List
Vector
Stack
Linked
List
CSE 2011
Prof. J. Elder
- 40 -
CSE 2011
Prof. J. Elder
- 41 -
Interface
Abstract Class
Collection
Class
List
Abstract
Collection
Queue
Abstract
List
Abstract
Queue
Priority
Queue
Abstract
Sequential
List
Array
List
Vector
Stack
Linked
List
CSE 2011
Prof. J. Elder
- 42 -
CSE 2011
Prof. J. Elder
- 43 -
CSE 2011
Prof. J. Elder
- 44 -
Interface
Abstract Class
Collection
Class
List
Abstract
Collection
Queue
Abstract
List
Abstract
Queue
Priority
Queue
Abstract
Sequential
List
Array
List
Vector
Stack
Linked
List
CSE 2011
Prof. J. Elder
- 45 -
CSE 2011
Prof. J. Elder
- 46 -
Interface
Abstract Class
Collection
Class
List
Abstract
Collection
Queue
Abstract
List
Abstract
Queue
Priority
Queue
Abstract
Sequential
List
Array
List
Vector
Stack
Linked
List
CSE 2011
Prof. J. Elder
- 47 -
Provides an iterator
We will study this in detail when we get to heaps!
CSE 2011
Prof. J. Elder
- 48 -
Interface
Abstract Class
Collection
Class
List
Abstract
Collection
Queue
Abstract
List
Abstract
Queue
Priority
Queue
Abstract
Sequential
List
Array
List
Vector
Stack
Linked
List
CSE 2011
Prof. J. Elder
- 49 -
Summary
From this lecture you should understand:
The purpose and advantages of the Java Collections Framework
How interfaces, abstract classes and classes are used
hierarchically to achieve some of the key goals of object-oriented
software engineering.
The purpose of iterators, and how to create and use them.
How the Java Collections Framework can be used to develop
code using general collections, lists, array lists, stacks and
queues.
CSE 2011
Prof. J. Elder
- 50 -
CSE 2011
Prof. J. Elder
- 51 -