{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

14b-map-grammar

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

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

CSE 143 Lecture 14 (B) Maps and Grammars reading: 11.3 slides created by Marty Stepp http://www.cs.washington.edu/143/

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

View Full Document
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?
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"

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

View Full Document
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" index 0 1 2 3 4 5 6 7 8 9 value 3 1 3 0 0 0 0 1 0 2 key "M" "O" "I" value 16 14 3 "M" "O" "I" 16 3 14 keys values
5 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.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

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
Ask a homework question - tutors are online