1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

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.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
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.
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.
Data Structures & Java Collections Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
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.
1 Frameworks Part 2. 2 Collections Framework Java API contains library of useful data structures Collections library also serves as framework for adding.
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.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Chapter 3 Introduction to Collections – Stacks Modified
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, 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.
1 Unit 10 - Data Structures Objectives 1. Describe the usage of data structures. 2. Develop simple data structures. 3. Apply Java collections. 4. Apply.
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.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
LinkedList Many slides from Horstmann modified by Dr V.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
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.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
1 Stacks (Continued) and Queues Array Stack Implementation Linked Stack Implementation The java.util.Stack class Queue Abstract Data Type (ADT) Queue ADT.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
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.
CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
1 Copyright © 2011 Tata Consultancy Services Limited COLLECTIONS By TEAM 5 Rajendhiran Sivan Christi Yashwanth Bijay Smruthi Satyajit.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
Collections Dwight Deugo Nesa Matic
1 Example: LinkedStack LinkedStack UML Class Diagram LinkedStack Class LinkedStack Attributes/Constructor LinkedStack Methods LinkedStack iterator method.
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
CS 151: Object-Oriented Design December 3 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Linked Data Structures
ADT’s, Collections/Generics and Iterators
Top Ten Words that Almost Rhyme with “Peas”
Introduction to Collections
TCSS 143, Autumn 2004 Lecture Notes
TCSS 342, Winter 2006 Lecture Notes
Introduction to Collections
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
Introduction to Collections
Collections Framework
Introduction to Collections
JCF Collection classes and interfaces
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Part of the Collections Framework
Java Generics & Iterators
A type is a collection of values
Presentation transcript:

1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable Collections Introduction to Project 1 Reading: L&C 3.1, 7.1, and Project 1

2 Collections A collection is a typical Abstract Data Type A collection is a class that contains and allows access to a group of objects The access “strategy” is in accordance with an abstract idea of the model for the collection –A Stack is a pile of objects, LIFO –A Queue is a waiting line of objects, FIFO –A Set is a group of objects (no duplicates) –A List is an indexed group of objects (duplicates OK)

3 The Java Collections API The classes in the Java Collections Library are named to indicate the underlying data structure and the abstract type of access 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 A 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 implements Collection Hence, polymorphism allows us to do these: ArrayList a = new ArrayList (); Collection c = a; // widening ArrayList d = (ArrayList ) c;

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 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 ArrayList Unique Methods Indexing is NOT a feature of all collections It is a unique feature of the ArrayList class Additional ArrayList 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)

8 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 which extends the Iterable interface Each subclass of the Collection interface is designed to support a specific model for access to the contents of the collection –Stack, Queue, Set, List, etc.

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

10 An Example Collection: Stack A stack is a linear collection where the elements are added or removed from the same end The access strategy is last in, first out (LIFO) The last element put on the stack is the first element removed from the stack Think of a stack of cafeteria trays

11 A Conceptual View of a Stack Top of Stack Adding an ElementRemoving an Element

12 Iterating over a Collection If we need to write code that retrieves all the elements of a collection to process them one at a time, we may use the “Iterator” design pattern from Design Patterns, Gamma et al. We call this iterating over the collection Collection extends Iterable (which is another interface) The Iterable interface requires one method: Iterator iterator();

13 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

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

15 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 access to the contents of an Iterable object ArrayList bookList = new ArrayList (); // Code to add some Books to the bookList Iterator itr = bookList.iterator(); while (itr.hasNext()) System.out.println (itr.next()); for (Book myBook : bookList) System.out.println (myBook);

16 Introduction to Project 1 In Project 1, you need to implement or add code to: –An Iterable class that contains the N 2 cells of a Kenken –A Cell class to represent each cell in the puzzle –An Iterator class that returns arrays of cells in the order of their rows, columns, and constraints –An isSolution method that iterates over the puzzle and determines if each row, column, and constraint is valid The Iterator will return each cell two times: –In an array representing its row –In an array representing its column It will return a valid or an invalid Cell array for each constraint

UML Class Diagram for Project 1 KenkenValidator + main (args: String [ ] ) : void + KenkenValidator(name : String) + isSolution() : boolean > Iterable + iterator() : Iterator > Iterator + hasNext() : boolean + next() : Object + remove() : void Kenken - puzzle : Cell [ ] [ ] - constraints : Constraint [] + Kenken(file : Scanner) + toString() : String KenkenIterator {Kenken inner class} - cursor : int - cursorConstraint : int + KenkenIterator () Cell - value : int + Cell() + setValue(value : int) : void + getValue(): int + toString() : String > Constraint > - value : int - op : char - array : Cell[ ] + Constraint (attributes) + isValid () : boolean

UML Sequence Diagram for Project 1 18 KenkenValidatorCell objects KenkenuIterator object Instantiates Instantiates N 2 Cell objects Instantiates Kenken object iterator() return reference hasNext() next() getValue() return true/false return int value return Cell [ ] return reference For-Each loop in isSolution() return references return reference Color green is used for constructors

19 Introduction to Project 1 In Project 1 with an NxN array: hasNext( ) returns true 3xN times next( ) returns a one dimensional Cell [N] for N rows, N columns, and ~N constraints 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

20 Introduction to Project 1 For a KenkenIterator class: Class Header could be: public class KenkenIterator implements Iterator { private Cell [][] puzzle; private int cursor; private int cursorConstraint; Constructor could be: public KenkenIterator() { cursor = cursorConstraint = 0; }

21 Introduction to Project 1 For a “rows only” KenkenIterator class: KenkenIterator hasNext method could be: public boolean hasNext() { return cursor < puzzle.length; } KenkenIterator next method could be: public Cell [] next() { // careful: this returns an alias of the Cell [] value = puzzle[cursor]; // row cursor++; return value; }