Summary of Class Presentation for July 1, 1999 Topic: Collections and Enumerators Modern software development requires the programming language to provide an extensive set of data structures and algorithms for manipulating those structures. The most common include: arrays, linked lists, stacks, sets, and maps. Java provided a minimal set until version 1.2 ( also called Java 2) which expanded that set greatly. Modern thinking requires that the internal implementation of the data structure be hidden from the client code. Each aggregate data structure can be associated with an iterator which hides how the internal structure is traversed to access all the elements of the data structure. Instead, the client code uses a simple iterator interface to request each element from the data structure until all the elements have been accessed. It is important to realize that the iterator builds a snapshot of the data structure at the time the iterator is built. Subsequent changes to the data structure ( addition or deletion of elements) is not reflected in the iterator. To get access to these changes, a new iterator must be created and used. In Java, all collections are objects. They each contain elements of type Object. Thus any object of any class can be upcasted to membership in a collection. Downcasting must be used whenever it is necessary to utilize the special methods or data fields of the specific class for any particular element. This downcasting is type-checked at execution time automatically using the runtime type identification facilities. If you attempt to downcast an object to a type which it does not possess, you get a runtime exception. Here is a simplified hierarchy for the new collections you definitely need to know about. Iterator Collection Map List Iterator List Set All of these are Interfaces. Two concrete classes which implement Set are ArraySet and HashSet. Two concrete classes which implement List are ArrayList and LinkedList. Two concrete classes which implement Map are ArrayMap and TreeMap. Normally, you would either create an object of one of these concrete classes, or extend one of these classes and then create an object of your newly defined class. Each Map can produce one or two collections. Each collection can produce an iterator. Each List can produce a ListIterator. A list can be accessed randomly. New elements may be inserted anywhere. The list has a definite ordering of its elements and a list iterator can be used to travel forwards or backwards through a list. A Set does not have any ordering. Each distinct element can appear in it only once. The iterator will give you elements in an order that you cannot control. A Map is like an array of pairs. The first element of each pair is a key used to retrieve the corresponding second element of that pair. Elements are only retrievable on the basis of the keys. Each key must be unique, but the second elements do not have to be unique. More details on these collections and iterators is provided in the summary of July 2nd's lecture.