Topic G JavaCollectionsFramework
Topic G JavaCollectionsFramework
and Reengineering
TOPIC G
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();
// Array Operations
Object[] toArray();
Object[] toArray(Object a[]);
}
// 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();
// Collection Views
• Hashtables are an example of public Set keySet();
Maps public Collection values();
public Set entrySet();
// Endpoints
Object first();
Object last();
// Comparator access
Comparator comparator();
}
Object firstKey();
Object lastKey();
}
Collection Map
AbstractCollection AbstractMap
List Set
Dictionary
AbstractList AbstractSet
Map
HashMap
AbstractSequentialList Hashtable
HashSet
LinkedHashMap
ArrayList
SortedMap
LinkedHashSet SortedSet
LinkedList
TreeMap
Vector TreeSet
Stack
AbstractSequentialList
ArrayList
LinkedList Vector
Stack
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
LinkedHashSet SortedSet
TreeSet
Set
• LinkedHashSet differs from HashSet
in that the order is maintained
AbstractSet
binary tree
- A binary tree is a tree in which each AbstractCollection
node has at most two children
Set
SortedMap
• Unlike HashTable, HashMap’s
methods are not synchronized
TreeMap
LinkedHashMap
• Less efficient than HashMap for
SortedMap
insertion and mapping
TreeMap
• Its methods are not synchronized
Map
• The class Hashtable was
introduced in JDK 1.0 and uses Hashtable
Enumerations instead of
Iterators
http://java.sun.com/j2se/1.4.2/docs/guide/collections/inde
x.html
http://java.sun.com/j2se/1.4.2/docs/api/