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

Slides:



Advertisements
Similar presentations
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Advertisements

Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 7 Object Oriented Programming in Java Advanced Topics Collection.
15-Jun-15 Lists in Java Part of the Collections Framework.
Algorithm Programming Containers in Java Bar-Ilan University תשס " ו by Moshe Fresko.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
Cmp Sci 187: Midterm Review Based on Lecture Notes.
1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.
CS 307 Fundamentals of Computer Science ADTS and Generic Data Structures 1 Topic 12 ADTS, Data Structures, Java Collections and Generic Data Structures.
12-Jul-15 Lists in Java Part of the Collections Framework.
The Collections Framework A Brief Introduction. Collections A collection is a structured group of objects –An array is a kind of collection –A Vector.
Java's Collection Framework
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Sets and Maps Part of the Collections Framework. The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
The Java Collections Framework (JCF) Introduction and review 1.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Chapter 18 Java Collections Framework
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
CS1101: Programming Methodology
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
This recitation 1 An interesting point about A3: Using previous methods to avoid work in programming and debugging. How much time did you spend writing.
Java Programming: From the Ground Up Chapter 17 The Java Collections Framework.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
List data type(ADT). Lists Elements : a 1,a 2,a 3,… a i-1,a i, a i+1,…a n Null List contains: 0 elements Types of Operations on list 1.Insertion 2.Deletion.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Topic 13 Iterators. 9-2 Motivation We often want to access every item in a data structure or collection in turn We call this traversing or iterating over.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
4-Mar-16 Introduction to Collections. Revision questions True false questions 0 for False 1 for True Please do not answer anything other than the above.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 9 Doubly Linked Lists and Ordered Lists Lecture.
Collections Dwight Deugo Nesa Matic
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
ADT’s, Collections/Generics and Iterators
Implementing ArrayList Part 1
TCSS 342, Winter 2006 Lecture Notes
Programming in Java Lecture 11: ArrayList
Copyright ©2012 by Pearson Education, Inc. All rights reserved
ArraySet Methods and ArrayIterator
CSE 143 Lecture 27: Advanced List Implementation
Collections Framework
JCF Collection classes and interfaces
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
TCSS 143, Autumn 2004 Lecture Notes
Part of the Collections Framework
Java Generics & Iterators
Copyright ©2012 by Pearson Education, Inc. All rights reserved
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 Introduction to Exercise 1

2 The Collection Interface Any class that implements the Collection interface can contain 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;

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

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

5 ArrayList Unique Methods Any methods involving indexing: void add(int index, T o) boolean addAll(int index, Collection c) T get(int index) int indexOf(T element) int lastIndexOf(T element) T remove(int index) T set(int index, T element) Indexing is NOT a feature of all collections It is a unique feature of the ArrayList class

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

8 The Collection Class Hierarchy > AbstractList > AbstractCollection > Collection > AbstractSet > AbstractQueue ArrayList > List

9 Iterating over a Collection Many times, we need to write code that retrieves the elements of a collection in a fashion according to its access model In Java, we refer to this as iterating over the collection Collection extends Iterable (which is another interface) so let’s look at what that means

10 Iterable Objects and Iterators An Iterable object allows you obtain an Iterator object to retrieve objects from it Iterator iterator() returns an Iterator object to access this Iterable group of objects An Iterator object allows you to retrieve a sequence of T objects using two methods: boolean hasNext() returns true if there are more objects of type T available in the group T next() returns the next T object from the group

11 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 (Project 1) 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:

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

13 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 but does not implement Iterable while (bookList.hasNext()) System.out.println (bookList.next());

14 Use of Exceptions In some cases, there may be constraints that prevent the execution of a method for a specific instance of a collection object For example, a remove operation can not be performed on a collection that is empty A remove method for a set collection may be coded to check for an empty set If an empty set is found, it may throw an EmptySetException

15 Use of Exceptions Hence, a method of a collection object may have a throws clause in its header T remove() throws EmptySetException To throw an exception, the method uses: throw new EmptySetException(“string”); The exception itself is defined as a class: public class EmptySetException extends RuntimeException { public EmptySetException (String set) { super ("The " + set + " is empty."); }

16 Introduction to Exercise 1 Sudoku puzzles are quite the rage today Solving a Sudoku puzzle involves filling a set of numbers into an NxN array so there is exactly one number of each value 1-N in each row, column, and N 1/2 xN 1/2 box You won’t need to write a program that solves Sudoku puzzles! In Exercise 1, you will write a program that validates the solution of a Sudoku puzzle.

17 Introduction to Exercise 1 In Exercise 1, you need to implement: –An Iterable class to contain the N 2 cells of a Sudoku puzzle and a Cell class to represent each cell –An Iterator class that returns arrays of cells in the order of their rows, columns, and N 1/2 xN 1/2 boxes –An isSolution method that iterates over the puzzle and determines if each row, column, and N 1/2 xN 1/2 box is valid The Iterator will return each cell three times: –In its row –In its column –In its N 1/2 xN 1/2 box

18 UML Class Diagram for Project 1 SudokuValidator + main (args: String [ ] ) : void - isSolution(puzzle : Sudoku) : bool > Iterable + iterator() : Iterator > Iterator + hasNext() : bool + next() : Object + remove() : void Sudoku - puzzle : Cell [ ] [ ] - Sudoku() + Sudoku(file : Scanner) + toString () : String SudokuIterator Cell - value : int - Cell() +Cell(value : int) +setValue(value : int) +getValue() : int > - puzzle : Cell [ ][ ] - cursor : int {See assignment text} - SudokuIterator () + SudokuIterator (puzzle : Cell [ ][ ])

19 Introduction to Project 1 To loop through a two dimensional array using for statements is relatively intuitive: Cell [][] puzzle = new Cell[size][size];... for (int i = 0; i < size; i++) for (int j = 0; j < size; j++) //statements using puzzle[i][j] It is obvious that this algorithm is O(n 2 ) for n = size

20 Introduction to Project 1 If an Iterator on a collection with an internal NxN array returns one element each time, the algorithm appears to have only one loop and be O(n), but it is not! The code using the Iterator object has: while (iterator.hasNext()) //statements using iterator.next() The hasNext() method returns true N 2 times The next() method returns N 2 times - each time with another Cell from the NxN array Hence, it is O(N 2 ) with respect to N

21 Introduction to Project 1 However, in Project 1 with an NxN array: hasNext( ) returns true 3xN times next( ) returns a one dimensional Cell [ ] of length N for N rows, N columns, and N boxes The iteration process g(N) is 3xN and is O(n) However, the code calling the hasNext( ) and next ( ) methods processes N elements each time for overall g(N) = 3NxN or O(N 2 ) again

22 Introduction to Project 1 For a “rows only” SudokuIterator class: Class Header could be: public class SudokuIterator implements Iterator { private Cell [][] puzzle; private int SIZE; private int cursor; Constructor could be: public SudokuIterator(Cell [][] puzzle) { this.puzzle = puzzle; SIZE = puzzle.length; cursor = 0; }

23 Introduction to Project 1 For a “rows only” Interator class: Code in Iterator hasNext method could be: public boolean hasNext() { return cursor < SIZE; } Code in Iterator next method could be: public Cell [] next() { value = puzzle[cursor]; cursor++; return value; }