Collection Iterable and Iterator Interfaces The Collection Interface
Collection, Iterable, and Iterator Interfaces • • The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable Collections Reading: L&C 3 rd: 15. 1 -15. 2 2 nd: 3. 2 1
Collections • A collection is a typical example of Abstract Data Type. • A collection is a data type that contains and allows access to a group of objects. • The Collection ADT is the most general form of ADTs designed for containing/accessing a group of objects. • We have more specific forms of Collection ADTs which describe the access “strategy” that 2 models that collection:
The Java Collections API • The classes and interfaces in the Java Collections Library are named to indicate the underlying data structure and the abstract Data type. • For example, the Array. List we studied in CS 110 uses an underlying array as the data structure for storing its objects and implements its access model as a list • However, from the user’s code point of view, 3 the data structure is hidden by the API.
The Collection Interface • Any class that implements the Collection interface can contain/access a group of objects • The type of objects that a Collection class contains can be specified using generics <T> • Array. List class implements Collection • Hence, polymorphism allows us to do these: Array. List<String> a = new Array. List<String>(); Collection<String> c = a; // widening Array. List<String> d = (Array. List<String>) c; 4
Collection Interface Methods boolean add(T o) add. All(Collection c) void clear() boolean contains(T o) boolean contains. All(Collection c) boolean equals(Object o) int hash. Code() boolean is. Empty() 5
Collection Interface Methods Iterator iterator() boolean remove(T o) boolean remove. All(Collection c) boolean retain. All(Collection c) int size() Object [] to. Array() <T> T[] to. Array(T[] a) 6
The Collection Interface Hierarchy • Typically, the Collection interface is not implemented directly by a class • There is a hierarchy of interfaces that extend the Collection interface • Each subclass of the Collection interface is designed to support a specific model for access to the contents of the collection – List, Set, Stack, Queue, etc. 7
Java Collection Hierarchy Collection Abstract. Collection Tree. Set Abstract Class Interface Class Set List Abstract. Set Abstract. List Hash. Set Array. List
List Methods • boolean add(T elem) void add(int index, T elem) boolean add. All(int index, Collection c) T get(int index) T remove(int index) T set(int index, T element) int index. Of(Object o) int last. Index. Of(Object o) • Indexing is NOT a feature of all collections • It is a feature of the List ADT 9
Array. List methods • Array. List is a class that implements the methods of the List interface. Therefore, all these methods can be called on an Array. List object. Ex: Array. List<String> list = new Array. List(); list. add(“ABC”); list. add(“EFG”); System. out. println(list. get(1)); System. out. println(list. index. Of(“ABC”)); System. out. println(list. index. Of(“HJK”));
Array. List methods • Array. List is an actual class that implements the methods of the List interface. Therefore, all these methods can be called on an Array. List object. Ex: Array. List<String> list = new Array. List(); list. add(“ABC”); list. add(“EFG”); System. out. println(list. get(1)); System. out. println(list. index. Of(“ABC”)); System. out. println(list. index. Of(“HJK”));
The Collection Interface Hierarchy <<interface>> Iterable <<interface>> Collection <<interface>> List <<interface>> Set <<interface>> Sorted. Set <<interface>> Queue 12
Iterating over a Collection • Many times, we need to write code that retrieves all the elements of a collection one at a time in order to process them. • We call this iterating over the collection. • Java library has a way to conveniently accomplish the process of accessing (visiting or retrieving) each element of a collection once. • The core of this approach is based on two 13 interfaces:
Iterator<T> • An iterator over a collection allows programs to conveniently access each element of a collection once and only once without having to keep track of anything related to the process of iteration (visiting of each element) [an example of use of encapsulation]. • Methods of Iterator<T> interface: – boolean has. Next() – T next()
Iterator Interface • We don't create an object of type iterator by itself, but we ask the collection object to return us one: Array. List<Book> shelf = new Array. List<Book>(); … Iterator<Book> iter = shelf. iterator(); while(iter. has. Next()){ Book book = iter. next(); System. out. println(book. to. String()); } (use simple for loop)
Iterable<T> Interface • An object of type Iterable allows you obtain an Iterator object to retrieve its elements. It has only one method: Iterator<T> iterator() returns an Iterator object to access to the elements of this Iterable group of objects. • Collection interface extends the Iterable interface: Collection<T> extends Iterable<T> (which is another interface) 16
Iterable Objects and Iterators • Classes in the Java standard class library that implement the Collection interface are Iterable OR you can implement Iterable in a class that you define. • If book. List is an object of an Iterable class that contains Book objects, we can retrieve all the available Book objects in either of two ways: 17
Iterable Objects and Loops • We can obtain an Iterator object from an Iterable object and use it to retrieve all the items from the Iterable object indirectly: Iterator itr = book. List. iterator(); while (itr. has. Next()) System. out. println (itr. next()); • The Java 5. 0 for-each loop simplifies the repetitive processing of the items available from an Iterable object for (Book my. Book : book. List) System. out. println (my. Book); 18
Iterators and Loops • If book. List is an object of an Iterator class that contains Book objects, you can access all the available objects directly: while (book. List. has. Next()) System. out. println (book. List. next()); • You can not use a “for-each” loop on an object of a class that only implements Iterator() method but does not implement Iterable. 19
- Slides: 19