Java Collections in Detail
Java Collections in Detail
Java Collections in Detail
// Basic operations
int size();
boolean isEmpty();
Iterator<E> iterator();
// Bulk operations
// Array operations
Object[] toArray();
}
A note on iterators
// Basic operations
int size();
boolean isEmpty();
Iterator<E> iterator();
// Bulk operations
// Array Operations
Object[] toArray();
// Search
int indexOf(Object o);
int lastIndexOf(Object o);
// Iteration
ListIterator<E> listIterator();
ListIterator<E> listIterator(int index);
// Range-view
List<E> subList(int from, int to);
}
A note on ListIterators
The three methods that ListIterator inherits from Iterator (hasNext, next, and remove)
do exactly the same thing in both interfaces. The hasPrevious and the previous
operations are exact analogues of hasNext and next. The former operations refer to the
element before the (implicit) cursor, whereas the latter refer to the element after the
cursor. The previous operation moves the cursor backward, whereas next moves it
forward.
The nextIndex method returns the index of the element that would be returned by a
subsequent call to next, and previousIndex returns the index of the element that would
be returned by a subsequent call to previous
The set method overwrites the last element returned by next or previous with the
specified element.
The add method inserts a new element into the list immediately before the current
cursor position.
Map — an object that maps keys to values. A Map cannot contain duplicate
keys; each key can map to at most one value. If you've used Hashtable,
you're already familiar with the basics of Map.
public interface Map<K,V>
public interface Map<K,V> {
// Basic operations
V put(K key, V value);
V get(Object key);
V remove(Object key);
boolean containsKey(Object key);
boolean containsValue(Object value);
int size();
boolean isEmpty();
// Bulk operations
void putAll(Map<? extends K, ? extends V> m);
void clear();
// Collection Views
public Set<K> keySet();
public Collection<V> values();
public Set<Map.Entry<K,V>> entrySet();
// Endpoints
E first();
E last();
// Comparator access
Comparator<? super E> comparator();
}
Note on Comparator
interface
Comparator is another interface (in addition to
Comparable) provided by the Java API which can be
used to order objects.
You can use this interface to define an order that is
different from the Comparable (natural) order.
public interface
SortedMap<K,V>
extends
Map<K,V>
SortedMap — a Map that maintains its mappings in ascending key order.
This is the Map analog of SortedSet. Sorted maps are used for naturally
ordered collections of key/value pairs, such as dictionaries and telephone
directories.
public interface
SortedMap<K,V>
extends Map<K,V>
public interface SortedMap<K, V> extends Map<K, V>{
Interfaces Implementations
Tree
Hash table Resizable array
(sorted)
Linked list Hash table + Linked list
TreeSet
Set HashSet
(sorted)
LinkedHashSet
LinkedList
List ArrayList
Queue
TreeMap
Map HashMap
(sorted)
LinkedHashMap
-----------------------------------------------------------------------------------------
import java.util.*;
Iterator i = ss.iterator();
while (i.hasNext())
System.out.println(i.next());
}
}
import java.util.*;
System.out.println(theMap);
System.out.println("--------------------------------------");
System.out.println(theMap.get("Korth, Evan"));
System.out.println(theMap.get("Franti, Michael"));
}
Other implementations in the API
Wrapper implementations delegate all their
real work to a specified collection but add (or
remove) extra functionality on top of what the
collection offers.
SynchronizationWrappers
Unmodifiable Wrappers
Convenience implementations are mini-
implementations that can be more convenient
and more efficient than general-purpose
implementations when you don't need their full
power
List
View of an Array
Immutable Multiple-Copy List
Immutable Singleton Set
Empty Set, List, and Map Constants
SortedSet TreeSet
Collection AbstractCollection
Vector Stack
List AbstractList
ArrayList
AbstractSequentialList LinkedList
SortedMap TreeMap
Copyright: Liang
Making your own
implementations
Most of the time you can use the implementations
provided for you in the Java API.
In case the existing implementations do not satisfy your
needs, you can write your own by extending the
abstract classes provided in the collections framework.
algorithms
The collections framework also provides polymorphic
versions of algorithms you can run on collections.
Sorting
Shuffling
Routine Data Manipulation
Reverse
Fill copy
etc.
Searching
Binary Search
Composition
Frequency
Disjoint
Finding extreme values
Min
Max