HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura...
Transcript of HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura...
![Page 1: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/1.jpg)
HashMaps
CS106A, Summer 2019Sarai Gould && Laura Cruz-Albrecht
Lecture 19
With inspiration from slides created by Keith Schwarz, Mehran Sahami, Eric Roberts, Stuart Reges, Chris Piech and others.
![Page 2: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/2.jpg)
Announcements
● Blank code is available to download from the website.
2
![Page 3: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/3.jpg)
Announcements
● Midterm grades go out on Gradescope after lecture!○ You will receive an email in your Stanford email inbox.○ Regrade Requests are due by next Monday, Aug. 5th at 10am.○ Sample solutions are online.
● ParaKarel was due at 10am.● Assignment 5 goes out right after lecture!
○ If you work as a pair, submit as a pair on Paperless!
3
![Page 4: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/4.jpg)
Learning Goal for Today
Know how to store data in and retrieve data from a
HashMap
4
![Page 5: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/5.jpg)
Plan for Today
● Review: Arrays, 2D Arrays, ArrayLists● HashMaps and Animal Sounds● Example: Interesting Dictionary● Example: CountingWords● Example: Swapping Keys and Values
5
![Page 6: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/6.jpg)
Review 2D: Arrays
int[][] matrix = new int[3][4];
6
# rows # columns
![Page 7: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/7.jpg)
Review 2D: Arrays
int[][] matrix = new int[3][4];
7
2
1
000010203
00010203
00010203
0 0 0 0 0
0 0 0 0
0
0
0
1
0
2
0
3
1
2
An array of arrays A unit (ie, a grid/matrix)
![Page 8: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/8.jpg)
Review: 2D Arrays For Loops
● The canonical way to loop over a 2D array is with a double for loop
type[][] arr = …for (int row = 0; row < numRows(arr); row++) {
for (int col = 0; col < numCols(arr); col++) {
// do something with arr[row][col] ...
}
}
8
![Page 9: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/9.jpg)
9
Review: images are 2D arrays of pixels.
![Page 10: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/10.jpg)
Our First ArrayList
ArrayList<String> myArrayList = new ArrayList<String>();
10
Type of thing your ArrayList will store Same type here.
![Page 11: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/11.jpg)
Our First ArrayList
ArrayList<String> myArrayList = new ArrayList<>();
11
Type of thing your ArrayList will store
Can optionally leave empty because of
type inference
![Page 12: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/12.jpg)
● An ordered, resizable list of information● Can add and remove elements (among other cool functionality)● Homogenous● Can store any Object type● Access individual items by index
When you don’t know how many are coming to the party
12
1
hedgehogPartyList
0 1
3
2
3
3
I love ArrayLists!! I brought all my friends
![Page 13: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/13.jpg)
Data Structures so Far
What if we want to associate multiple types of data together?
An arrays and ArrayLists can only store one data type at a time. How can we create a relationship between data?
13
![Page 14: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/14.jpg)
Data Structures so Far
List: ArrayList<Type>
Array: type[]
Matrix/2D Array: type[][]
Note: All of these are Objects and passed by reference.14
![Page 15: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/15.jpg)
HashMaps!
HashMaps have a map of keys -> values.
If you look up a key in a HashMap, it will give you the associated value back!
15
![Page 16: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/16.jpg)
Our First HashMap
HashMap<String, String> firstMap = new HashMap<String, String>();
16
![Page 17: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/17.jpg)
Our First HashMap
HashMap<String, String> firstMap = new HashMap<String, String>();
17
Data Type of “keys” in our HashMap
![Page 18: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/18.jpg)
Our First HashMap
HashMap<String, String> firstMap = new HashMap<String, String>();
18
Data Type of “values” in our HashMap
![Page 19: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/19.jpg)
Our First HashMap
HashMap<String, String> firstMap = new HashMap<String, String>();
19
Repeated types of key->value pairs
![Page 20: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/20.jpg)
Our First HashMap
HashMap<String, Integer> firstMap = new HashMap<String, Integer>();
20
Like ArrayLists, only stores objects
![Page 21: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/21.jpg)
Our First HashMap
HashMap<String, String> firstMap = new HashMap<>();
21
Or, we can leave this blank because of type inference!
![Page 22: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/22.jpg)
Our First HashMap
import java.util.*;
HashMap<String, String> firstMap = new HashMap<String, String>();
22
![Page 23: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/23.jpg)
Our First HashMap
import java.util.*;
HashMap<String, String> firstMap = new HashMap<String, String>();
23
Let’s create a HashMap that maps from animal names to animal sounds!
Basically, we’re creating a dictionary where, if we look up an animal’s name, it will tell us which sound it makes!
![Page 24: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/24.jpg)
Our First HashMap
import java.util.*;
HashMap<String, String> sounds = new HashMap<String, String>();
sounds.put(“dog”, “woof”);
24
sounds
“dog” “woof”
key value
![Page 25: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/25.jpg)
Our First HashMap
import java.util.*;
HashMap<String, String> sounds = new HashMap<String, String>();
sounds.put(“dog”, “woof”);
sounds.put(“cat”, “meow”);
25
sounds
“dog” “woof” “cat” “meow”
key value key value
![Page 26: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/26.jpg)
Our First HashMap
import java.util.*;
HashMap<String, String> sounds = new HashMap<String, String>();
sounds.put(“dog”, “woof”);
sounds.put(“cat”, “meow”);
// what does the fox say?*sounds.put(“fox”, “ring-ding-ding-ding-dingeringeding”);
26
sounds
“dog” “woof” “cat” “meow” “fox” “ring-ding-ding-ding-dingeringeding”
* Music reference… Not just crazy.
key value key value key value
![Page 27: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/27.jpg)
Our First HashMap
import java.util.*;
HashMap<String, String> sounds = new HashMap<String, String>();
sounds.put(“dog”, “woof”);
sounds.put(“cat”, “meow”);
// what does the fox say?*
sounds.put(“fox”, “ring-ding-ding-ding-dingeringeding”);
27
sounds
“dog” “woof” “cat” “meow”
* Music reference… Not just crazy.
“fox” “ring-ding-ding-ding-dingeringeding”
key value key value key value
![Page 28: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/28.jpg)
Our First HashMap
import java.util.*;
HashMap<String, String> sounds = new HashMap<String, String>();
sounds.put(“dog”, “woof”);
sounds.put(“cat”, “meow”);
// what does the fox say?*
sounds.put(“fox”, “ring-ding-ding-ding-dingeringeding”);
String dogSound = sounds.get(“dog”);
28
sounds
“dog” “woof” “cat” “meow”
* Music reference… Not just crazy.
“fox” “ring-ding-ding-ding-dingeringeding”
key value key value key value
![Page 29: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/29.jpg)
Our First HashMap
import java.util.*;
HashMap<String, String> sounds = new HashMap<String, String>();
sounds.put(“dog”, “woof”);
sounds.put(“cat”, “meow”);
// what does the fox say?*
sounds.put(“fox”, “ring-ding-ding-ding-dingeringeding”);
String dogSound = sounds.get(“dog”);
println(dogSound);
29
sounds
“dog” “woof” “cat” “meow”
* Music reference… Not just crazy.
woof
“fox” “ring-ding-ding-ding-dingeringeding”
key value key value key value
![Page 30: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/30.jpg)
Our First HashMap
import java.util.*;
HashMap<String, String> sounds = new HashMap<String, String>();
sounds.put(“dog”, “woof”);
sounds.put(“cat”, “meow”);
// what does the fox say?*
sounds.put(“fox”, “ring-ding-ding-ding-dingeringeding”);
String dogSound = sounds.get(“dog”);
println(dogSound);
println(sounds.get(“cat”));
30
sounds
“dog” “woof” “cat” “meow”
* Music reference… Not just crazy.
woofmeow
“fox” “ring-ding-ding-ding-dingeringeding”
key value key value key value
![Page 31: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/31.jpg)
Our First HashMap
import java.util.*;
HashMap<String, String> sounds = new HashMap<String, String>();
sounds.put(“dog”, “woof”);
sounds.put(“cat”, “meow”);
// what does the fox say?*
sounds.put(“fox”, “ring-ding-ding-ding-dingeringeding”);
String dogSound = sounds.get(“dog”);
println(dogSound);
println(sounds.get(“cat”));
println(sounds.get(“hippopotamus”)); // this isn’t in our map!
31
sounds
“dog” “woof” “cat” “meow”
* Music reference… Not just crazy.
woofmeownull
“fox” “ring-ding-ding-ding-dingeringeding”
key value key value key value
![Page 32: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/32.jpg)
What Does This Code Do?
import java.util.*;
HashMap<String, Integer> numLimbs = new HashMap<>();
numLimbs.put(“dog”, 4);
numLimbs.put(“snail”, 1);
// Ooops
numLimbs.put(“octopus”, 9);
// fixed
numLimbs.put(“octopus”, 8);
println(numLimbs.get(“dog”));
println(numLimbs.get(“snail”));
println(numLimbs.get(“octopus”));
32
numLimbs
![Page 33: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/33.jpg)
What Does This Code Do?
import java.util.*;
HashMap<String, Integer> numLimbs = new HashMap<>();
numLimbs.put(“dog”, 4);
numLimbs.put(“snail”, 1);
// Ooops
numLimbs.put(“octopus”, 9);
// fixed
numLimbs.put(“octopus”, 8);
println(numLimbs.get(“dog”));
println(numLimbs.get(“snail”));
println(numLimbs.get(“octopus”));
33
numLimbs
“dog” 4 “snail” 1 “octopus” 8
418
Writing .put(“octopus”, … ) a second time will
overwrite the first octopus value!
key value key value key value
![Page 34: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/34.jpg)
More HashMap Commands
Make a HashMapHashMap<keyType, valueType> myMap = new HashMap<keyType, valueType>();
Put and get values into a mapmyMap.put(key, value);myMap.get(key) // returns the corresponding value
Some useful other methodsint size = myMap.size();myMap.containsKey(key); // returns true or false if key is in mapmyMap.keySet(); // gets list of keysmyMap.remove(key); // removes a key from a HashMap
Iterate using a foreach loopfor(keyType key : myMap.keySet()){ // not ordered, so don’t expect it to be sorted! myMap.get(key); // do something with the key/value pair}
34
![Page 35: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/35.jpg)
What Does This Code Do?
import java.util.*;
HashMap<String, Integer> numLimbs = new HashMap<>();
numLimbs.put(“dog”, 4);
numLimbs.remove(“dog”);
numLimbs.put(“snail”, 1);
numLimbs.put(“snail”, 105);
numLimbs.put(“octopus”, 8);
println(numLimbs.get(“dog”));
println(numLimbs.get(“snail”));
println(numLimbs.get(“octopus”));
35
numLimbs
![Page 36: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/36.jpg)
What Does This Code Do?
import java.util.*;
HashMap<String, Integer> numLimbs = new HashMap<>();
numLimbs.put(“dog”, 4);
numLimbs.remove(“dog”);
numLimbs.put(“snail”, 1);
numLimbs.put(“snail”, 105);
numLimbs.put(“octopus”, 8);
println(numLimbs.get(“dog”));
println(numLimbs.get(“snail”));
println(numLimbs.get(“octopus”));
36
numLimbs
“snail” 105 “octopus” 8
null1058
key value key value
![Page 37: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/37.jpg)
What Does This Code Do?
import java.util.*;
HashMap<String, Integer> numLimbs = new HashMap<>();
numLimbs.put(“dog”, 4);
numLimbs.remove(“dog”);
numLimbs.put(“snail”, 1);
numLimbs.put(“snail”, 105);
numLimbs.put(“octopus”, 8);
println(numLimbs.get(“dog”));
println(numLimbs.get(“snail”));
println(numLimbs.get(“octopus”));
37
numLimbs
“snail” 105 “octopus” 8
null1058
Writing .remove(“dog”) will completely remove
“dog” from our HashMap.
key value key value
![Page 38: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/38.jpg)
What Does This Code Do?
import java.util.*;
HashMap<String, Integer> numLimbs = new HashMap<>();
numLimbs.put(“dog”, 4);
numLimbs.remove(“dog”);
numLimbs.put(“snail”, 1);
numLimbs.put(“snail”, 105);
numLimbs.put(“octopus”, 8);
println(numLimbs.get(“dog”));
println(numLimbs.get(“snail”));
println(numLimbs.get(“octopus”));
38
numLimbs
“snail” 105 “octopus” 8
null1058
Writing .put(“snail”, … ) a second time will
overwrite the first snail value!
key value key value
![Page 39: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/39.jpg)
You Mentioned Dictionaries...
39
Hey, I just found this new word today… “Kerfuffle”. Do you know what it means?
![Page 40: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/40.jpg)
Let’s Look Up Kerfuffle in Our Dictionary!
I have an “Interesting Dictionary” file that probably contains that word!
Let’s write a program to read the file into a HashMap, so we can easily ask our HashMap what the definition of Kerfuffle is!
40
![Page 41: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/41.jpg)
Our file is full of Strings and formatted as follows:
WordDefinitionWordDefinitionWordDefinition
41
Let’s Look Up Kerfuffle in Our Dictionary!
Pseudocode:
![Page 42: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/42.jpg)
Our file is full of Strings and formatted as follows:
WordDefinitionWordDefinitionWordDefinition
42
Let’s Look Up Kerfuffle in Our Dictionary!
Pseudocode:
open filewhile there are lines in the file: use first line as key in my map use second line as value for that keyclose file
while user types in another word ask which word they would like the def of print the associated value/definition from map!
Key Value
Key Value
Key Value
![Page 43: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/43.jpg)
Let’s Code It!
43
![Page 44: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/44.jpg)
Code: Printing from our HashMap
44
![Page 45: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/45.jpg)
Important Note: HashMaps are...
Objects!
This means that HashMaps are passed by reference. If we pass a HashMap into a method, we can change it without needing to return the HashMap.
45
![Page 46: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/46.jpg)
???
46
What else can you use HashMaps for?
![Page 47: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/47.jpg)
Counting Words
Another common use for HashMaps is for counting occurrences of something.
Let’s write a program to count words in some files I’ve provided. Based on the most common words, can you guess what the files are?
47
![Page 48: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/48.jpg)
Let’s Code It!
48
![Page 49: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/49.jpg)
49
Code: Counting Words
![Page 50: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/50.jpg)
HashMaps Aren’t Sorted!
Remember, HashMaps are not sorted. That’s why our words and counts weren’t in any specific order!
50
![Page 51: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/51.jpg)
Extra Exercise: Swapping Keys & Values
Can you write a program which will take a HashMap and swap the keys and values so that they keys are now the values and the values are now the keys?
51
![Page 52: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/52.jpg)
Let’s Code It!
52
![Page 53: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/53.jpg)
Code: Swapping Keys & Values
53
![Page 54: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/54.jpg)
Recap
● ArrayLists are a variable type representing a list of items ● Unlike arrays, ArrayLists have:
○ The ability to resize dynamically ○ Useful methods you can call on them
● Unlike ArrayLists, arrays have: ○ The ability to store any type of item, not just Objects
● HashMaps are a variable type representing a key-value pairs. ● Unlike arrays and ArrayLists, HashMaps:
○ Are not ordered ○ Store information associated with a key of any Object type
54
![Page 55: HashMaps - Stanford University€¦ · HashMaps CS106A, Summer 2019 Sarai Gould && Laura Cruz-Albrecht Lecture 19 With inspiration from slides created by Keith Schwarz, Mehran Sahami,](https://reader033.fdocuments.in/reader033/viewer/2022043006/5f8dd3cb5855d327605b7e54/html5/thumbnails/55.jpg)
Plan for Today
● Review: Arrays, 2D Arrays, ArrayLists● HashMaps and Animal Sounds● Example: Interesting Dictionary● Example: CountingWords● Example: Swapping Keys and Values
Next Time: More HashMaps and Sets!
55