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
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
Java Collections Framework
Collections Interfaces
• Java allows Collection
Map
using:
• Lists
• Sets SortedMap
• Hash tables Set List
(maps)
SortedSet
Interface: Collection
• The Collection interface is the root public interface Collection {
of the collection hierarchy // Basic Operations
int size();
boolean isEmpty();
• Some Collection implementations boolean contains(Object element);
allow boolean add(Object element);
- duplicate elements and others do not boolean remove(Object element);
- the elements to be ordered or not Iterator iterator();
• JDK doesn't provide any direct // Bulk Operations
implementations of this interface boolean containsAll(Collection c);
boolean addAll(Collection c);
- It provides implementations of more
boolean removeAll(Collection c);
specific sub interfaces like Set and List
boolean retainAll(Collection c);
void clear();
// Array Operations
Object[] toArray();
Object[] toArray(Object a[]);
}
SEG4110 - Topic G - Java Collections
Framework
Interface: Set
• A Set is a collection that public interface Set {
// Basic Operations
cannot contain duplicate int size();
elements boolean isEmpty();
boolean contains(Object
element);
• A set is not ordered boolean add(Object element);
- however, some subsets
boolean remove(Object
maintain order using extra data element);
structures Iterator iterator();
// Bulk Operations
• This is similar to the
boolean
mathematical concept of sets containsAll(Collection c);
boolean addAll(Collection
c);
boolean removeAll(Collection
c);
boolean retainAll(Collection
c);
void clear();
// Array
SEG4110 - Topic Operations
G - Java Collections
Object[] toArray();
Framework
Interface: List
• A List is ordered (also public interface List extends
Collection {
called a sequence) // Positional Access
Object get(int index);
Object set(int index, Object
• Lists can contain duplicate element);
elements void add(int index, Object
element);
Object remove(int index);
• The user can access abstract boolean addAll(int
index, Collection c);
elements by their integer
index (position) // Search
int indexOf(Object o);
int lastIndexOf(Object o);
// Iteration
ListIterator listIterator();
ListIterator listIterator(int
index);
// Range-view
List subList(int from, int to);
SEG4110
} - Topic G - Java Collections
Framework
public interface Map {
// Basic Operations
Interface: Map Object put(Object key, Object
value);
Object get(Object key);
• A Map is an object that maps
Object remove(Object key);
keys to values boolean containsKey(Object
key);
boolean containsValue(Object
• Maps cannot contain value);
duplicate keys int size();
boolean isEmpty();
• Each key can map to at most // Bulk Operations
void putAll(Map t);
one value void clear();
// Collection Views
• Hashtables are an example of public Set keySet();
Maps public Collection values();
public Set entrySet();
// Interface for entrySet
elements
public interface Entry {
Object getKey();
Object getValue();
SEG4110 - Topic G Object
- Java setValue(Object
Collections
value);
Framework
Interface: SortedSet
• A SortedSet is a Set that public interface SortedSet
maintains its elements in an extends Set {
// Range-view
order SortedSet subSet(Object
fromElement, Object toElement);
SortedSet headSet(Object
• Several additional operations toElement);
are provided to take SortedSet tailSet(Object
advantage of the ordering fromElement);
// Endpoints
Object first();
Object last();
// Comparator access
Comparator comparator();
}
SEG4110 - Topic G - Java Collections
Framework
Interface: SortedMap
• A SortedMap is a Map that public interface SortedMap
maintains its mappings in extends Map {
Comparator comparator();
ascending key order
SortedMap subMap(Object
fromKey, Object toKey);
• The SortedMap interface is SortedMap headMap(Object
used for apps like dictionaries toKey);
and telephone directories SortedMap tailMap(Object
fromKey);
Object firstKey();
Object lastKey();
}
SEG4110 - Topic G - Java Collections
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
Framework
Java Lists: LinkedList
Collection
• Java uses a doubly-linked list
- it can be traversed from the beginning or AbstractCollection
List
the end
• LinkedList provides methods to get, AbstractList
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 queue ArrayList
• LinkedList is not synchronized LinkedList Vector
- problems if multiple threads access a list
concurrently Stack
- LinkedList must be synchronized externally
SEG4110 - Topic G - Java Collections
Framework
Java Lists: ArrayList
Collection
• Uses an array to store the elements
AbstractCollection
List
• In addition to the methods of the
interface List AbstractList
• it provides methods to manipulate the
size of the array (e.g. ensureCapacity)
AbstractSequentialList
• More efficient than LinkedList for ArrayList
methods involving indices – get(),
set() LinkedList Vector
• It is not synchronized Stack
SEG4110 - Topic G - Java Collections
Framework
Java Lists: Vector
Collection
• Same as an the class ArrayList
AbstractCollection
List
• The main difference is that:
- The methods of Vector are AbstractList
synchronized
AbstractSequentialList
ArrayList
LinkedList Vector
Stack
SEG4110 - Topic G - Java Collections
Framework
Java Lists: Stack
Collection
• The Stack class represents a 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
• Vector operations should not be LinkedList Vector
accessible in a Stack
• It is designed this way because of Stack
a historical mistake
SEG4110 - Topic G - Java Collections
Framework
Java Sets: HashSet
• HashSet uses a hash table as the data Collection
structure to represent a set
AbstractCollection
• HashSet is a good choice for representing Set
sets if order of the elements is not
important AbstractSet
• HashSet methods are not synchronized
HashSet
LinkedHashSet SortedSet
TreeSet
SEG4110 - Topic G - Java Collections
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 is maintained
AbstractSet
• Performance is below that of
HashSet, due to the expense of HashSet
maintaining the linked list
LinkedHashSet SortedSet
• Its methods are not synchronized
TreeSet
SEG4110 - Topic G - Java Collections
Framework
Java Sets: TreeSet
• Stores the elements in a balanced Collection
binary tree
- A binary tree is a tree in which each AbstractCollection
node has at most two children
Set
• TreeSet elements are sorted
AbstractSet
• Less efficient than HashSet in
insertion due to the use of a binary HashSet
tree
LinkedHashSet SortedSet
• Its methods are not synchronized
TreeSet
SEG4110 - Topic G - Java Collections
Framework
Java Maps: HashMap
Map
• Stores the entries in a hash
table AbstractMap
• Efficient in inserting (put())
and retrieving elements (get())
HashMap
• The order is not maintained
LinkedHashMap
SortedMap
• Unlike HashTable, HashMap’s
methods are not synchronized
TreeMap
SEG4110 - Topic G - Java Collections
Framework
Java Maps: LinkedHashMap
Map
• Hash table and linked list
implementation of the Map
interface AbstractMap
• LinkedHashMap differs from
HashMap in that the order is
HashMap
maintained
• Performance is below that of LinkedHashMap
HashMap, due to the expense of SortedMap
maintaining the linked list
TreeMap
• Its methods are not synchronized
SEG4110 - Topic G - Java Collections
Framework
Java Maps: TreeMap
Map
• Red-Black tree based
implementation of the SortedMap AbstractMap
interface
• The keys are sorted according to
their natural order HashMap
LinkedHashMap
• Less efficient than HashMap for
SortedMap
insertion and mapping
TreeMap
• Its methods are not synchronized
SEG4110 - Topic G - Java Collections
Framework
The class Hashtable
• Same as HashMap except that its
methods are synchronized Dictionary
Map
• The class Hashtable was
introduced in JDK 1.0 and uses Hashtable
Enumerations instead of
Iterators
SEG4110 - Topic G - Java Collections
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
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
Framework
References
The Java Collections Framework:
http://java.sun.com/j2se/1.4.2/docs/guide/collections/inde
x.html
The Java 1.4.2 APIs:
http://java.sun.com/j2se/1.4.2/docs/api/
SEG4110 - Topic G - Java Collections
Framework