SEG4110 – Advanced Software Design
and Reengineering
TOPIC G
Java Collections Framework
Collections Frameworks
•A collection is an object that represents a
group of objects
•A collections framework is a unified
architecture for representing and manipulating
collections
- the manipulation should be independent of the
implementation details
•Advantages of having a collections framework
- reduces programming effort by providing useful data
structures and algorithms
- increases performance by providing high-performance data
structures and algorithms
- interoperability between the different collections
- Having a common language for manipulating the collections
SEG4110 - Topic G - Java Collections 2
Framework
The components of Java Collections Framework
from the user’s perspective
• Collection Interfaces: form the basis of the
framework
- such as sets, lists and maps
• Implementations Classes: implementations of the
collection interfaces
• Utilities - Utility functions for manipulating
collections such as sorting, searching…
• The Java Collections Framework also includes:
- algorithms
- wrapper implementations
- add functionality to other implementations such as
synchronization
SEG4110 - Topic G - Java Collections 3
Framework
Collections Interfaces
•Java allows Collection
Map
using:
• Lists
• Sets SortedMap
• Hash tables Set List
(maps)
SortedSet
SEG4110 - Topic G - Java Collections 4
Framework
Interface: Collection
• The Collection interface public interface Collection {
is the root of the // Basic Operations
collection hierarchy int size();
boolean isEmpty();
boolean contains(Object element);
• Some Collection boolean add(Object element);
implementations allow boolean remove(Object element);
- duplicate elements and Iterator iterator();
others do not
- the elements to be // Bulk Operations
ordered or not boolean containsAll(Collection c);
boolean addAll(Collection c);
boolean removeAll(Collection c);
• JDK doesn't provide any boolean retainAll(Collection c);
direct implementations of void clear();
this interface
- It provides implementations // Array Operations
of more specific sub Object[] toArray();
interfaces like Set and Object[] toArray(Object a[]);
List }
SEG4110 - Topic G - Java Collections 5
Framework
Interface: Set
• A Set is a public interface Set {
// Basic Operations
collection that int size();
cannot contain boolean isEmpty();
duplicate elements boolean contains(Object
element);
boolean add(Object element);
• A set is not ordered boolean remove(Object
- however, some subsets element);
maintain order using Iterator iterator();
extra data structures
// Bulk Operations
boolean
• This is similar to containsAll(Collection c);
boolean addAll(Collection
the mathematical c);
concept of sets boolean removeAll(Collection
c);
boolean retainAll(Collection
c);
void clear();
// Array
SEG4110 - Topic Operations
G - Java Collections 6
Framework Object[] toArray();
Interface: List
• A List is ordered public interface List extends
Collection {
(also called a // Positional Access
sequence) Object get(int index);
Object set(int index, Object
element);
• Lists can contain void add(int index, Object
element);
duplicate elements Object remove(int index);
abstract boolean addAll(int
index, Collection c);
• The user can access
elements by their // Search
int indexOf(Object o);
integer index
int lastIndexOf(Object o);
(position)
// Iteration
ListIterator listIterator();
ListIterator listIterator(int
index);
// Range-view
List subList(int from, int to);
SEG4110
} - Topic G - Java Collections 7
Framework
public interface Map {
// Basic Operations
Interface: Map Object put(Object key, Object
value);
Object get(Object key);
• A Map is an object Object remove(Object key);
that maps keys to boolean containsKey(Object
values key);
boolean containsValue(Object
value);
int size();
• Maps cannot contain
boolean isEmpty();
duplicate keys
// Bulk Operations
void putAll(Map t);
• Each key can map to void clear();
at most one value
// Collection Views
public Set keySet();
public Collection values();
• Hashtables are an public Set entrySet();
example of Maps
// Interface for entrySet
elements
public interface Entry {
Object getKey();
Object getValue();
SEG4110 - Topic G Object setValue(Object 8
- Java Collections
Frameworkvalue);
Interface: SortedSet
• A SortedSet is a Set public interface SortedSet
that maintains its extends Set {
// Range-view
elements in an order SortedSet subSet(Object
fromElement, Object toElement);
SortedSet headSet(Object
• Several additional toElement);
operations are SortedSet tailSet(Object
fromElement);
provided to take
advantage of the // Endpoints
ordering Object first();
Object last();
// Comparator access
Comparator comparator();
}
SEG4110 - Topic G - Java Collections 9
Framework
Interface: SortedMap
• A SortedMap is a Map public interface SortedMap
that maintains its extends Map {
Comparator comparator();
mappings in
ascending key order SortedMap subMap(Object
fromKey, Object toKey);
SortedMap headMap(Object
• The SortedMap toKey);
SortedMap tailMap(Object
interface is used fromKey);
for apps like
dictionaries and Object firstKey();
Object lastKey();
telephone }
directories
SEG4110 - Topic G - Java Collections 10
Framework
Java
Collections
Object
(java.lang)
Collection Map
AbstractCollection AbstractMap
List Set
Dictionary
AbstractList AbstractSet
Map
HashMap
AbstractSequentialList Hashtable
HashSet
LinkedHashMap
ArrayList
SortedMap
LinkedHashSet SortedSet
LinkedList
TreeMap
Vector TreeSet
Stack
SEG4110 - Topic G - Java Collections 11
Framework
Java Lists: LinkedList
Collection
• Java uses a doubly-linked list
- it can be traversed from the
AbstractCollection
beginning or the end List
• LinkedList provides methods to AbstractList
get, remove and insert an
element at the beginning and
end of the list AbstractSequentialList
- these operations allow a linked
list to be used as a stack or a ArrayList
queue
LinkedList Vector
• LinkedList is not synchronized
- problems if multiple threads
Stack
access a list concurrently
- LinkedList must be synchronized
externally
SEG4110 - Topic G - Java Collections 12
Framework
Java Lists: ArrayList
Collection
• Uses an array to store the
elements List
AbstractCollection
• In addition to the methods AbstractList
of the interface List
• it provides methods to
manipulate the size of the AbstractSequentialList
array (e.g. ensureCapacity)
ArrayList
• More efficient than
LinkedList for methods
LinkedList Vector
involving indices – get(),
set() Stack
• It is not synchronized
SEG4110 - Topic G - Java Collections 13
Framework
Java Lists: Vector
Collection
• Same as an the class
ArrayList List
AbstractCollection
• The main difference is AbstractList
that:
- The methods of Vector are
synchronized AbstractSequentialList
ArrayList
LinkedList Vector
Stack
SEG4110 - Topic G - Java Collections 14
Framework
Java Lists: Stack
• The Stack class represents a
Collection
last-in-first-out (LIFO) stack
of objects
AbstractCollection
List
• The common push and pop
operations are provided
AbstractList
• As well as a method to peek at
the top item on the stack is
also provided AbstractSequentialList
ArrayList
• Note: It is considered bad
design to make Stack a Subclass
of vector LinkedList Vector
• Vector operations should not
be accessible in a Stack
• It is designed this way Stack
because of a historical
mistake
SEG4110 - Topic G - Java Collections 15
Framework
Java Sets: HashSet
• HashSet uses a hash table as Collection
the data structure to
represent a set AbstractCollection
Set
• HashSet is a good choice for
representing sets if order AbstractSet
of the elements is not
important
HashSet
• HashSet methods are not synchronized
LinkedHashSet SortedSet
TreeSet
SEG4110 - Topic G - Java Collections 16
Framework
Java Sets: LinkedHashSet
Collection
• Hash table and linked list
implementation of the Set
interface AbstractCollection
Set
• LinkedHashSet differs from
HashSet in that the order AbstractSet
is maintained
• Performance is below that HashSet
of HashSet, due to the
expense of maintaining the LinkedHashSet SortedSet
linked list
• Its methods are not
TreeSet
synchronized
SEG4110 - Topic G - Java Collections 17
Framework
Java Sets: TreeSet
• Stores the elements in a Collection
balanced binary tree
- A binary tree is a tree in AbstractCollection
which each node has at
most two children Set
• TreeSet elements are AbstractSet
sorted
• Less efficient than HashSet
HashSet in insertion due
to the use of a binary LinkedHashSet SortedSet
tree
TreeSet
• Its methods are not
synchronized
SEG4110 - Topic G - Java Collections 18
Framework
Java Maps: HashMap
• Stores the entries in
Map
a hash table AbstractMap
• Efficient in
inserting (put()) and
retrieving elements HashMap
(get())
LinkedHashMap
• The order is not SortedMap
maintained
TreeMap
• Unlike HashTable,
HashMap’s methods are
not synchronized
SEG4110 - Topic G - Java Collections 19
Framework
Java Maps: LinkedHashMap
• Hash table and linked
Map
list implementation of
the Map interface AbstractMap
• LinkedHashMap differs
from HashMap in that
the order is HashMap
maintained
LinkedHashMap
• Performance is below SortedMap
that of HashMap, due
to the expense of
maintaining the linked
TreeMap
list
• Its methods are not
synchronized
SEG4110 - Topic G - Java Collections 20
Framework
Java Maps: TreeMap
• Red-Black tree based
Map
implementation of the
SortedMap interface
AbstractMap
• The keys are sorted
according to their HashMap
natural order
LinkedHashMap
• Less efficient than
SortedMap
HashMap for insertion
and mapping
TreeMap
• Its methods are not
synchronized
SEG4110 - Topic G - Java Collections 21
Framework
The class Hashtable
• Same as HashMap except
that its methods are Dictionary
synchronized
Map
• The class Hashtable Hashtable
was introduced in JDK
1.0 and uses
Enumerations instead
of Iterators
SEG4110 - Topic G - Java Collections 22
Framework
The Iterator Interface
• JCF provides a uniform way to iterate through the
collection elements using the Iterator Interface
• The Iterator interface contains the following
methods
- boolean hasNext(): returns true if the iteration has more
elements.
- Object next(): returns the next element in the iteration.
- void remove(): removes from the collection the last element
returned by the iterator
• Iterator replaces Enumeration in the Java
Collections Framework. Iterators differ from
enumerations in two ways:
- They allow the caller to remove elements from the
collection during the iteration with well-defined semantics
(a major drawback of Enumerations)
- Method names have been improved
SEG4110 - Topic G - Java Collections 23
Framework
The class Arrays
• This class contains various static methods for
manipulating arrays such as:
- Sorting, comparing collections, binary searching…
• Refer to:
http://java.sun.com/j2se/1.4.2/docs/api/ for a
complete list of utilities
SEG4110 - Topic G - Java Collections 24
Framework
References
The Java Collections Framework:
http://java.sun.com/j2se/1.4.2/docs/guid
e/collections/index.html
The Java 1.4.2 APIs:
http://java.sun.com/j2se/1.4.2/docs/api/
SEG4110 - Topic G - Java Collections 25
Framework