1 / 19

Collection, Iterable, and Iterator Interfaces

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.

yardley
Download Presentation

Collection, Iterable, and Iterator Interfaces

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 3rd: 15.1-15.2 2nd:3.2 1

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

  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() 5

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

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

  8. Java Collection Hierarchy Collection AbstractCollection Set List AbstractSet AbstractList TreeSet HashSet ArrayList Abstract Class Interface Class

  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 9

  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<String> 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<String> 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. The Collection Interface Hierarchy <<interface>> Iterable <<interface>> Collection <<interface>> List <<interface>> Set <<interface>> SortedSet <<interface>> Queue 12

  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<T> • Iterator<T> 13

  14. 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 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<Book> shelf = new ArrayList<Book>(); … Iterator<Book> iter = shelf.iterator(); while(iter.hasNext()){ Book book = iter.next(); System.out.println(book.toString()); } (use simple for loop)

  16. 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>extendsIterable<T> (which is another interface) 16

  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: 17

  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); 18

  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()); 19

More Related