Presentation is loading. Please wait.

Presentation is loading. Please wait.

Some Other Collections: Bags, Sets, Queues and Maps COMP 103 2015-T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.

Similar presentations


Presentation on theme: "Some Other Collections: Bags, Sets, Queues and Maps COMP 103 2015-T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington."— Presentation transcript:

1 Some Other Collections: Bags, Sets, Queues and Maps COMP 103 2015-T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington Thomas Kuehne  Thomas Kuehne, Marcus Frean,

2 RECAP  We met ArrayList and saw it was an implementation of the collection type List TODAY  A note on “Autoboxing”  Some further collection types: Bag, Set, Queue, Map Announcements:  Tutorials start today  Class reps 2 RECAP and TODAY

3 3 What can you put in a Collection ?  The type parameter of a collection can be any object type  private List creatures = new ArrayList ();  public void processStrings(Set strings){…  Set > allTasks = new HashSet > ();  What about collections of numbers, or booleans, or chars?  private List myNums = new ArrayList ();  public int computeScore(List answers){….  Must use “wrapper classes”:  Integer, Boolean, Double, Char, etc  private List myNums = new ArrayList ();  public int computeScore (List answers) {…. Problem: these are “primitive types”, not Objects Problem: these are “primitive types”, not object types

4 4 Collection of Primitive Types  You could “wrap” a values inside wrapper objects: List myNums = new ArrayList (); Integer num = new Integer(15); myNums.add(num);  And could write methods that “unwrap” again: int sum = 0; for (Integer num : myNums) { int n = num.intValue(); sum = sum + n; }  But this is just a pain!

5  Good news: Java will automatically wrap/unwrap private List myNumbers = new ArrayList (); myNumbers.add(15); int sum = 0; for (Integer num : myNumbers) { sum = sum + num; } OR for (int num : myNumbers){ sum = sum + num; }  Java Auto-boxes values if an object is expected, and  Auto-unboxes objects when values are expected Auto-boxing 5

6 Collections  Minimal Operations:  add(value) → true iff a collection was changed  remove(value) → true iff a collection was changed  contains(value) → true iff value is in bag, uses equal to test.  Plus … size(), isEmpty(), iterator(), clear(), addAll(collection)… 6

7 Sets  Set is a collection with  no structure or order maintained  no access constraints (access any item any time)  Only constraint is that duplicates are excluded  Operations (same as Bag, but different behaviour)  add(value) → true iff value was added (not a duplicate)  remove(value) → true iff value removed (was in set)  contains(value) → true iff value is in the set ……  Sets are very commonly used 7

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

9 Bags (aka Multi-Sets)  A Bag is a collection with  no structure or order maintained  no access constraints (access any item any time)  duplicates allowed  Examples A collection of current logged-on users. Bingo cards. The books in a book collection …  Good data structure for recording frequencies  The Java Collections Library does not feature “Bag”  9

10 Queues  Queues embody the principle of “First In, First Out” (FIFO)  Collection of values with an order  Constrained access: Only remove from the front  Two varieties: Ordinary queue: only add at the back Priority queue: ordering determined by priority of elements 10

11 Queues Many applications:  Operating Systems, Network Applications, multi-user systems Handling requests/events/jobs that must be done in order (often called a “buffer” in this context)  Simulation programs Representing queues in the real world (traffic, customers, deliveries, ….) Managing the events that must happen in the future  Search Algorithms breadth-first search Java Collection Library provides :  a Queue interface  several classes (i.e., implementations) : LinkedList, PriorityQueue,... 11

12 Queue Operations  offer(value) ⇒ boolean  add a value to the queue  (sometimes called “enqueue”; like “push” )  poll() ⇒ value  remove and return value at front/head of queue or null if the queue is empty  (sometimes called “dequeue”; like “pop”)  peek() ⇒ value  return value at head of queue, or null if queue is empty (doesn’t remove from queue)  remove() and element()  like poll() and peek(), but throw an exception if queue is empty  Question: Why not use List instead of Queue ? 12

13 Maps 13 Key Value Not this kind of MAP Something that MAPs a key to a value NameAge Bob25 Rita34 Sam15 Adam 6 : : :

14 Maps  Collection of data, but not of single values  Map = a Set of keys  value Pairs  get values via keys  No duplicate keys  Lots of implementations, most commonly used is HashMap “Sharon” ⇒ 228 “Becky” ⇒ 227 “Marcus” ⇒ 353 “Pondy” ⇒ 333 get (“Marcus”) put (“Sharon”, 336) 14 remove(“Becky”) put (“Pondy”, 333) 336

15 Maps  When declaring and constructing, must specify two types:  Type of the key, and type of the value private Map phoneBook; : phoneBook = new HashMap ();  Operations  get(key) → returns value associated with key (or null)  put(key, value) → sets the value associated with key (and returns the old value, if any)  remove(key) → removes the key and associated value (and returns the old value, if any)  containsKey(key) → returns whether the key is present  size() 15


Download ppt "Some Other Collections: Bags, Sets, Queues and Maps COMP 103 2015-T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington."

Similar presentations


Ads by Google