Download presentation
Presentation is loading. Please wait.
1
Some Collections: BAGS, SETS, and STACKS
COMP 103 Some Collections: BAGS, SETS, and STACKS
2
RECAP-TODAY 2 RECAP Type Parameters (any Object type, ‘wrapped’ primitive types) Autoboxing ArrayList and List example TODAY Iterators More Collections: Bags, Sets, Stacks
3
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!
4
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…
5
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, … …
6
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)…
7
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!!
8
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
9
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
10
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()
11
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!)
12
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”); }
13
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
14
Stack for evaluating expressions
14 (6 + 4) * ((10 * √64) – (√81/ 3)) ⇒ ( (6, 4)+ , ((10, (64)√) * , ((81)√, 3)/ )- )* ⇒ √ * 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”
15
Summary 15 Iterators Bags Sets Stacks
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.