Maps v v Maps are another way of

  • Slides: 13
Download presentation
Maps v v Maps are another way of organizing data Keys and Values q

Maps v v Maps are another way of organizing data Keys and Values q q q Comp. Sci 6 Each key maps to a value Some keys can map to the same value Can change the value a key maps to 35. 1

Example v Each student could be mapped to their favorite ice cream flavor Comp.

Example v Each student could be mapped to their favorite ice cream flavor Comp. Sci 6 35. 2

Implementing a Map v We will use Tree. Map in Java v Example: Map<String,

Implementing a Map v We will use Tree. Map in Java v Example: Map<String, String> fav = new Tree. Map<String, String>(); v Keys map to values Comp. Sci 6 35. 3

To use a Map v Put in a key and its value fav. put(”Forbes”,

To use a Map v Put in a key and its value fav. put(”Forbes”, ”Strawberry”); v Get a value for a key val = fav. get(”Forbes”); v Change value for key fav. put(”Astrachan”, ”Coffee Mocha”); Comp. Sci 6 35. 4

Change Astrachan’s value Comp. Sci 6 35. 5

Change Astrachan’s value Comp. Sci 6 35. 5

Value could be a set Comp. Sci 6 35. 6

Value could be a set Comp. Sci 6 35. 6

Classwork today v File of words q q q Comp. Sci 6 Determine number

Classwork today v File of words q q q Comp. Sci 6 Determine number times each words appears For each word, determine all line numbers it appears on For each alphabetical letter, determine all the words that start with that letter. 35. 7

First look at methods given v v main get. Wordcounts q q v Given

First look at methods given v v main get. Wordcounts q q v Given a Scanner bound to a file Return a Map of words to counts print. Results q Comp. Sci 6 Given a map print key followed by value 35. 8

Wordlines: get. Word. Counts public Map<String, Integer> get. Word. Counts (Scanner input) { Map<String,

Wordlines: get. Word. Counts public Map<String, Integer> get. Word. Counts (Scanner input) { Map<String, Integer> results = new Tree. Map<String, Integer>(); while (input. has. Next()) { String word = input. next(); Integer count = results. get(word); if (count == null) { results. put(word, 1); } else { results. put(word, count + 1); } } return results; } Comp. Sci 6 35. 9

Wordlines: print. Results public void print. Results(Map<String, ? > results) { for (String key

Wordlines: print. Results public void print. Results(Map<String, ? > results) { for (String key : results. key. Set()) { System. out. println(key + "t" + results. get(key). to. String()); } // OR: for (Map. Entry<String, ? > current : results. entry. Set()) { System. out. println(current. get. Key() + "t" + current. get. Value()); } } Comp. Sci 6 35. 10

Output Comp. Sci 6 35. 11

Output Comp. Sci 6 35. 11

Todo: get. Line. Numbers v Map each word to a set of line numbers

Todo: get. Line. Numbers v Map each word to a set of line numbers it occurs on Comp. Sci 6 35. 12

Todo: get. Frequencies v Map each letter of alphabet to words Comp. Sci 6

Todo: get. Frequencies v Map each letter of alphabet to words Comp. Sci 6 35. 13