Notes for Containers and Iterators This single lecture is designed to introduce the concept of containers (Java Collection and Map interfaces), discuss some particular example containers, and discuss iterators It's important to continue to emphasize that containers and iterators are OO concepts, not just Java things. One important point to remember when dealing with whole collections when dealing with inheritance hierarchies is that A is subtype of B, it is NOT the case that Collection<A> is a subtype of Collection<B>. This may not rear its ugly head, but some code can seem to not make sense because of this... I'll try not to touch this 1. Intro Containers hold objects Built-in arrays In Java - Collection (sequence of elements) and Map (key-value object pairs) 2. Collections Framework 3. Wildcards "? super T" was first shown in the generics lecture - an object of any ancestor of T "? extends T" is new - an object of any descendent of T "?" is new - an object of any kind -- shorthand for "? extends Object" These are used in some the method definitions. In a generic class such as MyClass<T> myMethod (ArrayList<? extends T>) means that myMethod accepts any ArrayList<> as long as the base type of ArrayList is a descendent of T 4. Selected Methods of Collection<T> "optional" methods mean that their implementation may throw the UnsupportedOperationException. Most of the optional methods are typically implemented Equals( Object x) is for the collection (ie to determine if two collections are "equal". There's no official constraints on equals( ), it should make intuitive sense for the collection The interface allows for easy intermixing of collections through the use of wildcards in parameters for some methods. containsAll(Collection<?> targets ) - ask if a collection with any base type is fully contained with the calling collection object addAll (Collection<? extends T> collectionToAdd ) - add a collection of objects (which may be subtypes of the base type) to the collection. "? extends T" makes sense here to limit the kind of objects you can add to the calling collection object removeAll( Collection< ?> collectionToRemove) - try to remove a collection of any kind of element from the calling object Note the description of add( ) and addAll( ) - they only ensure that X exists. This is to allow Set to prevent duplicates 5. Set<T> is an interface that extends Collection<T>. Sets have the same interface as collections, but with different semantics for add( ), addAll( ) -- no duplicates are allowed Just a collection with no duplicates 6. List<T> is an interface that extends Collection<T> An ordered (i.e. indexable) collection Some List methods have different semantics than Collection Extends Collection<T> by adding new methods ( 7 slides ) These methods add the notion of "index" to the collection 7. List example code Note the first line -- List<Integer> = new ArrayList<Integer>( ); By using List<> (the interface) rather that ArrayList<> (the class), we can easily change our implementation (to say LinkedList) by changing just that one line of code. It's typical to upcast the concrete class to its interface, then use the interface. There are some instances where this doesn't work (eg LinkedList extends List, so if you upcast you couldn't use the new LL methods) add( T x ) puts elements at the end remove( int k) removes element at index k add( int k, T element) inserts element at index k set( int k, T element) changes the value at index k to element 8. Classes that implement List<T> ArrayList and Vector are the basic array-like classes that have fast random access For these classes, add(index, element) and remove( ) will be slow (O(n)) due to shifting elements LinkedList is probably a double-linked list (the ListIterator is bi-directional) 9. Iterators Java only supports iterators for Collections (don't know why) so this material is placed here Iterator concept - general purpose way of sequencing through the elements Collections produce iterators via iterator( ) method Iterator<T> interface -- hasNext( ), next( ), remove( ) <optional> Example code from the text using HashSet<T> For-each vs iterator Multiple independent iterators; remove( ) 10. LinkedList Iterator Bi-directional, so new methods (previous, hasPrevious) to support this LinkedList cursor positions 11. Map Interface Maps hold ordered pairs, hence their definition uses two type parameter, typically K (for Key) and V ( for Value) Maps require unique Keys, but duplicate values may be assigned to different keys 12. Map Methods These are pretty straightforward, except for putAll( ). putAll( ) uses wildcards to place a bound on both K and V 13. Maps and Iterators Maps do not support iterator( ) method, so no direct iteration entrySet( ), keyset( ), values( ) return Set/Collection views that are iterable "view" means that the Set/Collection returned are backed by the map, so changes to the Set/Collection affect the map