Collections Blue
Collections Blue
Collections
What is the Collections
framework?
Collections framework provides two things:
– implementations of common high-level data structures: e.g. Maps,
Sets, Lists, etc.
– An organized class hierarchy with rules/formality for adding new
implementations
The latter point is the sense in which Collections are a
framework.
Note the difference between providing a framework +
implementation and just implementation.
Some other differences:
– code reuse
– clarity
– unit testing?
Definition of collection
A collection — sometimes called a container — is
simply an object that groups multiple elements
into a single unit.
Collections are used to store, retrieve, manipulate,
and communicate aggregate data.
SortedSet
• The Collection inteface stores groups of Objects,
with duplicates allowed
• The Set interface extends Collection but forbids
duplicates
• The List interface extends Collection, allows duplicates,
and introduces positional indexing.
• Map is a separate hierarchy
Collection implementations
Note that Java does not provide any direct
implementations of Collection.
Rather, concrete implementations are based
on other interfaces which extend Collection,
such as Set, List, etc.
Still, the most general code will be written
using Collection to type variables.
A Peek at generics
Old way
Here is the same example modified to use generics: Think “Collection of Strings”
// Removes the 4-letter words from c
static void expurgate(Collection<String> c) {
for (Iterator<String> i = c.iterator(); i.hasNext(); )
if (i.next().length() == 4) i.remove();
}
Generics
There are lots of little subtleties introduced
by this capability.
import java.util.*;
import java.util.*;