Java Class Libraries
One of Java's great strengths is its rich standard class library. Mastering standard libraries is a key skill for effective programming in any language. Unfortunately, these libraries are way too extensive to discuss in our class; fortunately, the API reference documentation is very accessible and usually well-written.
I'll discuss two of the most important subsets of the class libraries:
- Collections: for managing collections of objects (in package java.util).
- AWT: graphics and windowing classes (in package java.awt).
Caveat: Both the AWT and the collections framework have undergone significant changes between Java 1.0, 1.1, and 1.2. Current web browsers only support Java 1.1, so many features cannot be used in applets. Consult the library documentation if you are ever in doubt about the version of a library feature.
Collections
There are essentially three flavors of collections:
Lists Ordered sequences of elements: Vector, ArrayList, LinkedList. Sets Collections of elements whose uniqueness is guaranteed: HashSet, TreeSet Maps Not, strictly speaking, a collection, but a function that maps a a collection of keys onto a collection of values: HashMap, TreeMap. Collections share common interface features, of which the most significant is the iterator framework. An iterator abstracts the idea of iterating over a collection from the details of how collections are represented. Iterators are used as follows:
public void applyTo(Collection c) { Iterator i = c.iterator(); // Initializer while ( i.hasNext() ) { // Predicate: are we done? Object o = i.next(); // Accessor: get the element // ... do something } } As usual (in your TA's opinion), Java does iteration somewhat better than C++, and somewhat worse than Smalltalk.