1 / 53

Maps Chris Piech CS106A, Stanford University

This article explores the usage of HashMap in Java with a simple example. Learn how to add elements, retrieve values based on keys, and explore the keys and values of a HashMap.

clarav
Download Presentation

Maps Chris Piech CS106A, Stanford University

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Maps Chris Piech CS106A, Stanford University

  2. CS106A Winter 2018 Contest

  3. Why is this so fast?

  4. Where are we?

  5. Where are we? • Karel the Robot • Java • Console Programs • Graphics Programs • Text Processing • Data Structures • GUIs • Defining our own Variable Types

  6. Collections High Level List: ArrayList<type> Array: type[] Matrix: type[][]

  7. Collections High Level List: ArrayList<String> Array: double[] Matrix: int[][]

  8. ArrayList index -> value

  9. Arrays index -> value

  10. Matrix (row, col) -> value

  11. Maps can have any type for key Many examples

  12. HashMap key -> value

  13. Simple Example Make a new HashMap of animal sounds 2. Add elements: Put [key = “dog”, value = “bark”] Put [key=“cat”, value=“meow”] Put [key=“seal”, value=“owowow”] 3. Get elements: Get [key = “dog”]

  14. Simple Example Make a new HashMap of animal sounds 2. Add elements: Put [key = “dog”, value = “bark”] Put [key=“cat”, value=“meow”] Put [key=“seal”, value=“owowow”] 3. Get elements: Get [key = “dog”]

  15. Simple Example animalSoundMap Values: Keys: Make a new HashMap of animal sounds 2. Add elements: Put [key = “dog”, value = “bark”] Put [key=“cat”, value=“meow”] Put [key=“seal”, value=“owowow”] 3. Get elements: Get [key = “dog”]

  16. Simple Example animalSoundMap Values: Keys: Make a new HashMap of animal sounds 2. Add elements: Put [key = “dog”, value = “bark”] Put [key=“cat”, value=“meow”] Put [key=“seal”, value=“owowow”] 3. Get elements: Get [key = “dog”]

  17. Simple Example animalSoundMap Values: “bark” Keys: “dog” Make a new HashMap of animal sounds 2. Add elements: Put [key = “dog”, value = “bark”] Put [key=“cat”, value=“meow”] Put [key=“seal”, value=“owowow”] 3. Get elements: Get [key = “dog”]

  18. Simple Example animalSoundMap Values: “bark” Keys: “dog” Make a new HashMap of animal sounds 2. Add elements: Put [key = “dog”, value = “bark”] Put [key=“cat”, value=“meow”] Put [key=“seal”, value=“owowow”] 3. Get elements: Get [key = “dog”]

  19. Simple Example animalSoundMap Values: “meow” “bark” Keys: “dog” “cat” Make a new HashMap of animal sounds 2. Add elements: Put [key = “dog”, value = “bark”] Put [key=“cat”, value=“meow”] Put [key=“seal”, value=“owowow”] 3. Get elements: Get [key = “dog”]

  20. Simple Example animalSoundMap Values: “meow” “bark” Keys: “dog” “cat” Make a new HashMap of animal sounds 2. Add elements: Put [key = “dog”, value = “bark”] Put [key=“cat”, value=“meow”] Put [key=“seal”, value=“owowow”] 3. Get elements: Get [key = “dog”]

  21. Simple Example animalSoundMap “ow ow ow” Values: “meow” “bark” Keys: “dog” “cat” “seal” Make a new HashMap of animal sounds 2. Add elements: Put [key = “dog”, value = “bark”] Put [key=“cat”, value=“meow”] Put [key=“seal”, value=“owowow”] 3. Get elements: Get [key = “dog”]

  22. Simple Example animalSoundMap “ow ow ow” Values: “meow” “bark” Keys: “dog” “cat” “seal” Make a new HashMap of animal sounds 2. Add elements: Put [key = “dog”, value = “bark”] Put [key=“cat”, value=“meow”] Put [key=“seal”, value=“owowow”] 3. Get elements: Get [key = “dog”]

  23. Simple Example animalSoundMap “ow ow ow” Values: “meow” “bark” Keys: “dog” “cat” “seal” Make a new HashMap of animal sounds 2. Add elements: Put [key = “dog”, value = “bark”] Put [key=“cat”, value=“meow”] Put [key=“seal”, value=“owowow”] 3. Get elements: Get [key = “dog”]

  24. Simple Example animalSoundMap “ow ow ow” Values: “meow” “bark” Keys: “dog” “cat” “seal” Make a new HashMap of animal sound 2. Add elements: Put [key = “dog”, value = “bark”] Put [key=“cat”, value=“meow”] Put [key=“seal”, value=“owowow”] 3. Get elements: Get [key = “dog”]

  25. Simple Example animalSoundMap “ow ow ow” Values: “meow” “bark” Keys: “dog” “cat” “seal” Make a new HashMap of animal sound 2. Add elements: Put [key = “dog”, value = “bark”] Put [key=“cat”, value=“meow”] Put [key=“seal”, value=“owowow”] 3. Get elements: Get [key = “cat”]

  26. Simple Example animalSoundMap “ow ow ow” Values: “meow” “bark” Keys: “dog” “cat” “seal” Make a new HashMap of animal sound 2. Add elements: Put [key = “dog”, value = “bark”] Put [key=“cat”, value=“meow”] Put [key=“seal”, value=“owowow”] 3. Get elements: Get [key = “cat”]

  27. Simple Example animalSoundMap “ow ow ow” Values: “meow” “bark” Keys: “dog” “cat” “seal” Make a new HashMap of animal sound 2. Add elements: Put [key = “dog”, value = “bark”] Put [key=“cat”, value=“meow”] Put [key=“seal”, value=“owowow”] 3. Get elements: Get [key = “cat”]

  28. My First Map HashMap<String, String> animalSoundMap = newHashMap<String, String>();

  29. My First Map Value Type Key Type HashMap<String, String> animalSoundMap = newHashMap<String, String>();

  30. My First Map HashMap<String, String> animalSoundMap= newHashMap<String, String>();

  31. My First Map HashMap<String, String> animalSoundMap = new HashMap<String, String>();

  32. My First Map HashMap<String, String> animalSoundMap = newHashMap<String, String>(); animalSoundMap.put(“dog”, “bark”);

  33. My First Map HashMap<String, String> animalSoundMap = newHashMap<String, String>(); animalSoundMap.put(“dog”, “bark”);

  34. My First Map HashMap<String, String> animalSoundMap = newHashMap<String, String>(); animalSoundMap.put(“dog”, “bark”);

  35. My First Map HashMap<String, String> animalSoundMap = newHashMap<String, String>(); animalSoundMap.put(“dog”, “bark”);

  36. My First Map HashMap<String, String> animalSoundMap = newHashMap<String, String>(); animalSoundMap.put(“dog”, “bark”); animalSoundMap.get(“dog”);

  37. My First Map HashMap<String, String> animalSoundMap = newHashMap<String, String>(); animalSoundMap.put(“dog”, “bark”); animalSoundMap.get(“dog”);

  38. My First Map HashMap<String, String> animalSoundMap = newHashMap<String, String>(); animalSoundMap.put(“dog”, “bark”); animalSoundMap.get(“dog”);

  39. My First Map animalSoundMap “ow ow ow” Values: “meow” “bark” Keys: “dog” “cat” “seal” Make a new HashMap of animal sound 2. Add elements: Put [key = “dog”, value = “bark”] Put [key=“cat”, value=“meow”] Put [key=“seal”, value=“owowow”] 3. Get elements: Get [key = “dog”]

  40. My First Map animalSoundMap “ow ow ow” Values: “meow” “bark” Keys: “dog” “cat” “seal” // 1. Make a new map HashMap<String, String> animalSoundMap = newHashMap<String, String>(); // 2. Put things into the map animalSoundMap.put("dog", "woof"); animalSoundMap.put("cat", "meow"); animalSoundMap.put("seal", "owowow"); // 3. Get things out of the map animalSoundMap.get("dog"); // "woof”

  41. My First Map animalSoundMap “ow ow ow” Values: “meow” “bark” Keys: “dog” “cat” “seal” // 1. Make a new map HashMap<String, String> animalSoundMap = newHashMap<String, String>(); // 2. Put things into the map animalSoundMap.put("dog", "woof"); animalSoundMap.put("cat", "meow"); animalSoundMap.put("seal", "owowow"); // 3. Get things out of the map animalSoundMap.get("dog"); // "woof" animalSoundMap.get("fox"); // ?

  42. brothers Vegard and BårdYlvisåker Circa 2013

  43. Ylvis– “The Fox”. Permission asked. Pending.

  44. My First Map animalSoundMap “ow ow ow” Values: “meow” “bark” Keys: “dog” “cat” “seal” // 1. Make a new map HashMap<String, String> animalSoundMap = newHashMap<String, String>(); // 2. Put things into the map animalSoundMap.put("dog", "woof"); animalSoundMap.put("cat", "meow"); animalSoundMap.put("seal", "owowow"); // 3. Get things out of the map animalSoundMap.get("dog"); // "woof" animalSoundMap.get("fox"); // ?

  45. My First Map animalSoundMap “ow ow ow” Values: “meow” “bark” Keys: “dog” “cat” “seal” // 1. Make a new map HashMap<String, String> animalSoundMap = newHashMap<String, String>(); // 2. Put things into the map animalSoundMap.put("dog", "woof"); animalSoundMap.put("cat", "meow"); animalSoundMap.put("seal", "owowow"); // 3. Get things out of the map animalSoundMap.get("dog"); // "woof" animalSoundMap.get("fox"); // null

  46. HashMaps on one slide Make a HashMap Put and get values into a map Some useful other methods int size = myMap.size(); myMap.containsKey(key); // returns true or false if key is in map myMap.keySet(); myMap.remove(key); // make like a tree and leave! Iterate using a foreach loop HashMap<keyType, valueType> myMap = new HashMap<keyType, valueType>(); myMap.put(key, value); myMap.get(key) // returns the corresponding value for(keyType key : myMap.keySet()){ // not ordered myMap.get(key); // do something with the key/value pair}

  47. Phone Book 6701678

  48. Make a keyboard

  49. Why is this so fast?

  50. Why is this so fast? inthash(stringkey); * Learn more in CS106B

More Related