Must take picture of my cute face again I’m already cute.. Nvm! YAY! Act cute time! Cheese..
HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in...
Transcript of HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in...
![Page 1: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/1.jpg)
HashMap
![Page 2: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/2.jpg)
Friday Four Square Today!Outside Gates at 4:15PM
![Page 3: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/3.jpg)
Not All Data is Linear
![Page 4: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/4.jpg)
HashMap<String, String> myMap = new HashMap<String, String>();
![Page 5: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/5.jpg)
HashMap<String, String> myMap = new HashMap<String, String>();myMap.put("CS106A", "Cool");
CS106ACS106A Cool
To add a key/value pair to a HashMap, use the syntax
map.put(key, value)
![Page 6: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/6.jpg)
HashMap<String, String> myMap = new HashMap<String, String>();myMap.put("CS106A", "Cool");myMap.put("Ibex", "OMG SO CUTE");
CS106ACS106A Cool
CS106AIbexOMG
SO CUTE
![Page 7: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/7.jpg)
HashMap<String, String> myMap = new HashMap<String, String>();myMap.put("CS106A", "Cool");myMap.put("Ibex", "OMG SO CUTE");myMap.put("137", "Mysterious");
CS106ACS106A Cool
CS106AIbexOMG
SO CUTE
CS106A137 Mysterious
![Page 8: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/8.jpg)
HashMap<String, String> myMap = new HashMap<String, String>();myMap.put("CS106A", "Cool");myMap.put("Ibex", "OMG SO CUTE");myMap.put("137", "Mysterious");myMap.put("CS106A", "Awesome");
CS106ACS106A Cool
CS106AIbexOMG
SO CUTE
CS106A137 Mysterious
Awesome
If you put a key/value pair where
the key exists, the old value is replaced.
![Page 9: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/9.jpg)
HashMap<String, String> myMap = new HashMap<String, String>();myMap.put("CS106A", "Cool");myMap.put("Ibex", "OMG SO CUTE");myMap.put("137", "Mysterious");myMap.put("CS106A", "Awesome");
myMap.get("Ibex");
CS106ACS106A Awesome
CS106AIbexOMG
SO CUTE
CS106A137 Mysterious
![Page 10: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/10.jpg)
HashMap<String, String> myMap = new HashMap<String, String>();myMap.put("CS106A", "Cool");myMap.put("Ibex", "OMG SO CUTE");myMap.put("137", "Mysterious");myMap.put("CS106A", "Awesome");
myMap.get("Ibex");
CS106ACS106A Awesome
CS106AIbexOMG
SO CUTE
CS106A137 Mysterious
To look up the value associated with a key:
map.get(key)
![Page 11: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/11.jpg)
HashMap<String, String> myMap = new HashMap<String, String>();myMap.put("CS106A", "Cool");myMap.put("Ibex", "OMG SO CUTE");myMap.put("137", "Mysterious");myMap.put("CS106A", "Awesome");
myMap.get("Ibex");myMap.get("CS106A");
CS106ACS106A Awesome
CS106AIbexOMG
SO CUTE
CS106A137 Mysterious
![Page 12: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/12.jpg)
HashMap<String, String> myMap = new HashMap<String, String>();myMap.put("CS106A", "Cool");myMap.put("Ibex", "OMG SO CUTE");myMap.put("137", "Mysterious");myMap.put("CS106A", "Awesome");
myMap.get("Ibex");myMap.get("CS106A");
CS106ACS106A Awesome
CS106AIbexOMG
SO CUTE
CS106A137 Mysterious
![Page 13: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/13.jpg)
HashMap<String, String> myMap = new HashMap<String, String>();myMap.put("CS106A", "Cool");myMap.put("Ibex", "OMG SO CUTE");myMap.put("137", "Mysterious");myMap.put("CS106A", "Awesome");
myMap.get("Ibex");myMap.get("CS106A");myMap.get("KE$HA");
CS106ACS106A Awesome
CS106AIbexOMG
SO CUTE
CS106A137 Mysterious
![Page 14: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/14.jpg)
HashMap<String, String> myMap = new HashMap<String, String>();myMap.put("CS106A", "Cool");myMap.put("Ibex", "OMG SO CUTE");myMap.put("137", "Mysterious");myMap.put("CS106A", "Awesome");
myMap.get("Ibex");myMap.get("CS106A");myMap.get("KE$HA");
CS106ACS106A Awesome
CS106AIbexOMG
SO CUTE
CS106A137 Mysterious
If you get a key that isn't in a map, the
method returns null.
![Page 15: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/15.jpg)
HashMap<String, String> myMap = new HashMap<String, String>();myMap.put("CS106A", "Cool");myMap.put("Ibex", "OMG SO CUTE");myMap.put("137", "Mysterious");myMap.put("CS106A", "Awesome");
myMap.get("Ibex");myMap.get("CS106A");myMap.get("KE$HA");myMap.containsKey("137");
CS106ACS106A Awesome
CS106AIbexOMG
SO CUTE
CS106A137 Mysterious
You can check whether a key exists in the map:
map.containsKey(key)
![Page 16: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/16.jpg)
Basic HashMap Operations
● HashMap has two type arguments:
HashMap<KeyType, ValueType>
● To insert a key/value pair:
map.put(key, value)
● To look up the value associated with a key:
map.get(key)
● To check whether a key exists:
map.containsKey(key)
![Page 17: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/17.jpg)
Making HashMap Shine
![Page 18: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/18.jpg)
Exploring the US
![Page 19: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/19.jpg)
Making Music
![Page 20: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/20.jpg)
The Keyboard File Format
note-file-namexy
widthheight
is white key?
![Page 21: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/21.jpg)
The xkcd Color Survey
![Page 22: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/22.jpg)
![Page 23: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/23.jpg)
![Page 24: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/24.jpg)
![Page 25: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/25.jpg)
The xkcd Color Survey
● Volunteers (online) were shown a randomly-chosen color and asked to name the color.
● The result is (after filtering) about 2.8 million RGB triplets and their names.
● What do people think the colors are?
![Page 26: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/26.jpg)
The Color File Format
color-namered
greenblue
![Page 27: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/27.jpg)
Displaying Colors
● The HSB Color Format● Choose the hue (what
color), saturation (how intense), and brightness (absolute brightness).
● Each choice in the range from 0.0 to 1.0.
![Page 28: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/28.jpg)
How to Structure the Data?
blue 15 137 255 0 0 127 88 88 190
red 166 14 7 99 55 5 255 0 0
gray 154 156 157 243 242 254 140 143 148
associate each color namewith a list of RGB triplets
![Page 29: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/29.jpg)
How to Structure the Data?
blue 15 137 255 0 0 127 88 88 190
red 166 14 7 99 55 5 255 0 0
gray 154 156 157 243 242 254 140 143 148
HashMap<color name, list of RGB triplets>
![Page 30: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/30.jpg)
How to Structure the Data?
blue 15 137 255 0 0 127 88 88 190
red 166 14 7 99 55 5 255 0 0
gray 154 156 157 243 242 254 140 143 148
HashMap<String, list of RGB triplets>
![Page 31: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/31.jpg)
How to Structure the Data?
blue 15 137 255 0 0 127 88 88 190
red 166 14 7 99 55 5 255 0 0
gray 154 156 157 243 242 254 140 143 148
HashMap<String, ArrayList<RGB triplet>>
![Page 32: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/32.jpg)
How to Structure the Data?
blue 15 137 255 0 0 127 88 88 190
red 166 14 7 99 55 5 255 0 0
gray 154 156 157 243 242 254 140 143 148
BHashMap<String, ArrayList<int[]>>B
![Page 33: HashMap - web.stanford.edu · SO CUTE CS106A137 Mysterious You can check whether a key exists in the map: map.containsKey(key) Basic HashMap Operations](https://reader034.fdocuments.us/reader034/viewer/2022052019/603364a8c2176514594f505d/html5/thumbnails/33.jpg)
For More Information
http://blog.xkcd.com/2010/05/03/color-survey-results/