Topic G JavaCollectionsFramework
Topic G JavaCollectionsFramework
and Reengineering
TOPIC G
SortedSet
// 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();
}
Collection Map
AbstractCollection AbstractMap
List Set
Dictionary
AbstractList AbstractSet
Map
HashMap
AbstractSequentialList Hashtable
HashSet
LinkedHashMap
ArrayList
SortedMap
LinkedHashSet SortedSet
LinkedList
TreeMap
Vector TreeSet
Stack
queue
LinkedList Vector
• LinkedList is not synchronized
- problems if multiple threads
Stack
access a list concurrently
- LinkedList must be synchronized
externally
• It is not synchronized
that:
- The methods of Vector are
synchronized AbstractSequentialList
ArrayList
LinkedList Vector
Stack
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
Set
TreeSet
Set
is maintained
synchronized
sorted
• Efficient in
inserting (put()) and
retrieving elements HashMap
(get())
LinkedHashMap
maintained
TreeMap
• Unlike HashTable,
HashMap’s methods are
not synchronized
list implementation of
the Map interface AbstractMap
• LinkedHashMap differs
from HashMap in that
the order is HashMap
maintained
LinkedHashMap
list
implementation of the
SortedMap interface
AbstractMap
natural order
LinkedHashMap
synchronized
Map
• Refer to:
http://java.sun.com/j2se/1.4.2/docs/api/ for a
complete list of utilities
http://java.sun.com/j2se/1.4.2/docs/guid
e/collections/index.html
http://java.sun.com/j2se/1.4.2/docs/api/