Some Collections: BAGS, SETS, and STACKS

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Stacks, Queues, and Linked Lists
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.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
COMP 103 Linked Stack and Linked Queue.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
TCSS 342, Winter 2005 Lecture Notes
Java Collections. Collections, Iterators, Algorithms CollectionsIteratorsAlgorithms.
Java's Collection Framework
Collections. Why collections? Collections are used to hold a collection of objects. List holds objects based on order of insertion and can hold non unique.
SEG4110 – Advanced Software Design and Reengineering TOPIC G Java Collections Framework.
Java Collections Framework A presentation by Eric Fabricant.
COMP T2 Lecture 5 School of Engineering and Computer Science, Victoria University of Wellington Thomas Kuehne Maps, Stacks  Thomas Kuehne, Marcus.
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Topic 3 The Stack ADT.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
(c) University of Washington14-1 CSC 143 Java Collections.
Stacks and Queues Pepper. Why History Simplicity Operating Systems – Function Call Stack.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 More Collections: Queues,
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
COMP 103 Linked Lists. 2 RECAP-TODAY RECAP  Linked Structures: LinkedNode  Iterating and printing Linked Nodes  Inserting and removing Linked Nodes.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Data structures Abstract data types Java classes for Data structures and ADTs.
HIT2037- HIT6037 Software Development in Java 22 – Data Structures and Introduction.
Computer Science 209 Software Development Inheritance and Composition.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
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.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Click to edit Master text styles Stacks Data Structure.
COMP 103 Maps and Queues. RECAP  Iterators (for-each loop)  Bag, Sets, and Stacks - a class, not interface TODAY  Maps and Queues 2 RECAP-TODAY QUICK.
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Slides by Donald W. Smith
Using the Java Collection Libraries COMP 103 # T2
Building Java Programs
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Slides by Donald W. Smith
Stacks and Queues.
JAVA COLLECTIONS LIBRARY
JAVA COLLECTIONS LIBRARY
Software Development Inheritance and Composition
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
CSE 373: Data Structures and Algorithms
Stacks and Queues.
COMP 103 Maps, Stacks Thomas Kuehne 2016-T2 Lecture 5
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Building Java Programs
Road Map CS Concepts Data Structures Java Language Java Collections
Abstract Data Types (ADT)
Java Methods Stacks and Queues A & AB Object-Oriented Programming
Stacks and Queues.
Building Java Programs
Building Java Programs
Welcome to CSE 143! Go to pollev.com/cse143.
CSC 1052 Stacks 1/11/2019.
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
CSE 1020: The Collection Framework
Stacks Chapter 5.
Web Design & Development Lecture 6
CS 240 – Advanced Programming Concepts
Stacks and Queues.
Presentation transcript:

Some Collections: BAGS, SETS, and STACKS COMP 103 Some Collections: BAGS, SETS, and STACKS

RECAP-TODAY 2 RECAP Type Parameters (any Object type, ‘wrapped’ primitive types) Autoboxing ArrayList and List example TODAY Iterators More Collections: Bags, Sets, Stacks

You have already met one: scanner is a (rather fancy) iterator! Iterators List <String> items; for ( String str : items) UI.print(str + “, “); How does this work? The compiler essentially turns it into this: Iterator<String> iter = items.iterator( ); while (iter.hasNext( )){ String str = iter.next( ); UI.print(str + “, “); } Do Foe Woe Zoo Go Row So hasNext() next() An iterator object attached to items that will keep giving you the next element You have already met one: scanner is a (rather fancy) iterator!

Iterator is an Interface… 4 Set <Task> mySet = new ArraySet <Task> (); Iterator <Task> iter = mySet.iterator(); mySet iter Conforms to the interface Iterator so… it has methods hasNext() next() remove() Conforms to the interface Set so… has methods add() remove() contains(), etc…

Collections library Interfaces: Classes: = Bag (most general) 5 Interfaces: Collection <E > = Bag (most general) List <E > = ordered collection Set <E > = unordered, no duplicates [Stack <E > (not an interface!) ordered collection, limited access (add/remove at one end) ] Map <K, V > = key-value pairs (or mapping) Queue <E > (add at end, remove from front) Classes: List classes: ArrayList, LinkedList, Vector Set classes: HashSet, TreeSet, EnumSet, LinkedHashSet,… Stack classes: Stack Map classes: EnumMap, HashMap, TreeMap, LinkedHashMap, WeakHashMap, … …

Bags (“Collection”) A Bag is a collection with Minimal Operations: 6 A Bag is a collection with no structure or order maintained no access constraints (access any item any time) duplicates allowed Minimal Operations: add(value) → returns true iff a collection was changed remove(value) → returns true iff a collection was changed contains(value) → returns true iff value is in bag uses equal to test. findElement(value) → returns a matching item, iff in bag Plus size(), isEmpty(), iterator(), clear(), addAll(collection), removeAll(collection)…

Bag Applications When to use a Bag? 7 When to use a Bag? When there is no need to order a collection, and duplicates are possible: A collection of current logged-on users. Bingo cards. The books in a book collection … But not all that common! Typically we don’t really have duplicate items. There are no standard implementations of Bag!!

Sets Set is a collection with: 8 Set is a collection with: no structure or order maintained no access constraints (access any item any time) Only property is that duplicates are excluded Operations: (same as Bag, but different behaviour) add(value) → true iff value was added (eg, not duplicate) remove(value) → true iff value removed (was in set) contains(value) → true iff value is in the set findElement(value) → matching item, iff in value in the set … Sets are as common as Lists

check if a particular word is in the set Using Sets 9 Checking vocabulary of children’s books: private Set <String> words = new HashSet <String> (); public void readBook(Scanner sc){ while (sc.hasNext ()) words.add(sc.next()); } public void checkWord(String wd){ if (words.contains(wd)) UI.println(“Yes, ”+wd+“ is in the book”); else UI.println(“No, ”+wd+“ is not in the book”); make an empty set add words to it check if a particular word is in the set

Stacks Works on the “first in last out” (FILO) principle 10 Works on the “first in last out” (FILO) principle Stacks are a special kind of List: Sequence of values, Constrained access: add, get, and remove only from one end. Java should have a Stack interface, and different implementations of it (ArrayStack, LinkedStack, etc), but... In Java Collections library: Stack is a class that implements List (actually extends Vector) Has extra operations: push(value), pop(), peek()

Stacks Stacks have extra operations: push(value), pop(), peek() 11 Stacks have extra operations: push(value), pop(), peek() pop(): Removes and returns top of stack push(value): Put value on top of stack peek(): Returns top of stack, without removing plus the other List operations (oops!)

Stacks example Reversing the items from a file: 12 Reversing the items from a file: read and push onto a stack pop them off the stack public void reverseNums(Scanner sc){ Stack <Integer> myNums = new Stack <Integer> (); while (sc.hasNextInt()) myNums.push(sc.nextInt()) while (! myNums.isEmpty()) UI.print(myNums.pop() + “\n”); }

Applications of Stacks 13 Programs that deal with programs eg, program execution, eg, expression execution, (6 + 4) * ((10 * √64) – (√81/ 3)) working on subtasks, then returning to previous task. Processing files of structured (nested) data. eg. reading files of with structured markup (HTML, XML,…) Undo in editors

Stack for evaluating expressions 14 (6 + 4) * ((10 * √64) – (√81/ 3)) ⇒ ( (6, 4)+ , ((10, (64)√) * , ((81)√, 3)/ )- )* ⇒ 6 4 + 10 64 √ * 81 √ 3 / - * if number: push on stack if operator: pop arguments from stack compute value push on stack if done: pop answer from stack Postfix order (also known as “Reverse Polish notation”

Summary 15 Iterators Bags Sets Stacks