14b-map-grammar - CSE 143 Lecture 14 (B) Maps and Grammars...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 143 Lecture 14 (B) Maps and Grammars reading: 11.3 slides created by Marty Stepp http://www.cs.washington.edu/143/
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Exercise • Write a program to count the occurrences of each word in a large text file (e.g. Moby Dick or the King James Bible). – Allow the user to type a word and report how many times that word appeared in the book. – Report all words that appeared in the book at least 500 times, in alphabetical order. • How will we store the data to solve this problem?
Background image of page 2
3 The Map ADT map : Holds a set of unique keys and a collection of values, where each key is associated with one value. – a.k.a. "dictionary", "associative array", "hash" • basic map operations: put (key, value ): Adds a mapping from a key to a value. get (key ): Retrieves the value mapped to the key. remove (key ): Removes the given key and its mapped value. myMap.get("Juliet") returns "Capulet"
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Maps and tallying • a map can be thought of as generalization of a tallying array – the "index" (key) doesn't have to be an int • recall previous tallying examples from CSE 142 – count digits: 22092310907 // (M)cCain, (O)bama, (I)ndependent – count votes: "MOOOOOOMMMMMOOOOOOMOMMIMOMMIMOMMIO" i n d e x0123456789 v a l u e3130000102 key "M" "O" "I" value 16 14 3
Background image of page 4
Map implementation • in Java, maps are represented by Map interface in java.util •Map is implemented by the HashMap and TreeMap classes – HashMap : implemented using an array called a "hash table"; extremely fast: O(1) ; keys are stored in unpredictable order – TreeMap : implemented as a linked "binary tree" structure; very fast: O(log N) ; keys are stored in sorted order – A map requires 2 type parameters: one for keys, one for values. // maps from String keys to Integer values
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/04/2011 for the course CSE 143 taught by Professor Sr during the Spring '08 term at University of Washington.

Page1 / 18

14b-map-grammar - CSE 143 Lecture 14 (B) Maps and Grammars...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online