Summary of Class Presentation of July 2, 1999 Topic: more on Collections and Iterators Here are the methods supported by every Collection object ( including List and Set objects): boolean add(Object) returns false if the argument will not be present in the Collection after this add boolean addAll(Collection) adds all the elements in the Collection, Returns true if any elements were added. void clear( ) Removes all the elements in the collection boolean contains (Object) boolean containsAll ( Collection) boolean isEmpty ( ) Iterator iterator ( ) creates an iterator from this collection boolean remove(object) boolean removeAll(Collection) boolean retainAll(Collection) retains only elements in both the argument and the object doing the method. int size( ) Object [ ] toArray( ) converts the collection to an array of Objects. Note that addAll, clear, remove, removeAll, and retainAll are optional methods. They might not be implemented by some Collections. Lists are a very important collection. A list is ordered and new elements may be inserted at any position in the list. Elements may be deleted from any position in the list. There are two types of lists (implementation of the List Interface) that are commonly used: 1. ArrayList: replaces Vector in nearly all cases. Quick access, but slow modification ( adding or deleting elements). Should be used when the list is created and then not changed very much. 2. LinkedList: slower access to arbitrary elements, but much faster insertion and deletion compared to ArrayList. Should be used when you expect your list to be volatile ( lots of insertions and deletions). Lists differ from general Collections in the following ways: 1. Can use a listIterator or an Iterator; 2. Has additional methods including removeRange remove ( location) indexOf next add ( location, object) addAll ( location, collection) 3. LinkedList has additional methods including addFirst addLast getFirst getLast removeFirst removeLast for easy implementation of stacks, queues, and dequeues. Set is the other type of collection, There is no ord elements, but as new elements are added duplicates of existing values are not added. There are three types of Set implementation: 1. ArraySet efficient access when the number of elements is small ( about thirty or less). Insertions and deletions are slow. 2. HAshSet should be used for sets of more than 30 elements that do not need to be accessed in order of value. 3. TreeSet shuld be used for any size set that is usually accessed in order of ascending or descending value. A map is not a collection, but it is an aggregate of values. Each element in a map is a pair - a single object that is the key and another object that is the value. You can retrieve values by providing the keys. For each key, there must be a unique value in a map.