Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.

Similar presentations


Presentation on theme: "1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable."— Presentation transcript:

1 1 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.22 nd :3.2

2 2 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 models that collection: –A Set is a group of things without any duplicates –A Stack is the abstract idea of a pile of things, LIFO –A Queue is the abstract idea of a waiting line, FIFO –A List is an indexed group of things

3 3 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 ArrayList we studied in CS110 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, the data structure is hidden by the API.

4 4 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 ArrayList class implements Collection Hence, polymorphism allows us to do these: ArrayList a = new ArrayList (); Collection c = a; // widening ArrayList d = (ArrayList ) c;

5 5 Collection Interface Methods boolean add(T o) addAll(Collection c) void clear() boolean contains(T o) boolean containsAll(Collection c) boolean equals(Object o) int hashCode() boolean isEmpty()

6 6 Collection Interface Methods Iterator iterator() boolean remove(T o) boolean removeAll(Collection c) boolean retainAll(Collection c) int size() Object [] toArray() T[] toArray(T[] a)

7 7 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.

8 Java Collection Hierarchy Collection SetList AbstractCollection AbstractSetAbstractList TreeSetArrayListHashSet Interface Abstract Class Class

9 9 List Methods boolean add(T elem) void add(int index, T elem) boolean addAll(int index, Collection c) T get(int index) T remove(int index) T set(int index, T element) int indexOf(Object o) int lastIndexOf(Object o) Indexing is NOT a feature of all collections It is a feature of the List ADT

10 ArrayList methods ArrayList is a class that implements the methods of the List interface. Therefore, all the these methods can be called on an ArrayList object. Ex: ArrayList list = new ArrayList(); list.add(“ABC”); list.add(“EFG”); System.out.println(list.get(1)); System.out.println(list.indexOf(“ABC”)); System.out.println(list.indexOf(“HJK”));

11 ArrayList methods ArrayList is an actual class that implements the methods of the List interface. Therefore, all the these methods can be called on an ArrayList object. Ex: ArrayList list = new ArrayList(); list.add(“ABC”); list.add(“EFG”); System.out.println(list.get(1)); System.out.println(list.indexOf(“ABC”)); System.out.println(list.indexOf(“HJK”));

12 12 The Collection Interface Hierarchy > Collection > List > SortedSet > Iterable > Set > Queue

13 13 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 interfaces: –Iterable –Iterator

14 Iterator 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 interface: –boolean hasNext() –T next() –void remove()

15 Iterator Interface We don't create an object of type iterator by itself, but we ask the collection object to return us one: ArrayList shelf = new ArrayList (); … Iterator iter = shelf.iterator(); while(iter.hasNext()){ Book book = iter.next(); System.out.println(book.toString()); } (use simple for loop)

16 16 Iterable Interface An object of type Iterable allows you obtain an Iterator object to retrieve its elements. It has only one method: Iterator iterator() returns an Iterator object to access to the elements of this Iterable group of objects. Collection interface extends the Iterable interface: Collection extends Iterable (which is another interface)

17 17 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 bookList is an object of an Iterable class that contains Book objects, we can retrieve all the available Book objects in either of two ways:

18 18 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: The Java 5.0 for-each loop simplifies the repetitive processing of the items available from an Iterable object Iterator itr = bookList.iterator(); while (itr.hasNext()) System.out.println (itr.next()); for (Book myBook : bookList) System.out.println (myBook);

19 19 Iterators and Loops If bookList is an object of an Iterator class that contains Book objects, you can access all the available objects directly: You can not use a “for-each” loop on an object of a class that only implements Iterator() method but does not implement Iterable. while (bookList.hasNext()) System.out.println (bookList.next());


Download ppt "1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable."

Similar presentations


Ads by Google