1 / 12

MAPS

MAPS. MSTC CS2. What is a Map?. A java Collection NO INDICES Each value is assigned a key Each key is UNIQUE Ask the map for a certain key , and he will give you the corresponding value. Better mark it on my handy treasure map. I think I’ll hide my treasure here. x.

lara
Download Presentation

MAPS

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 MSTC CS2

  2. What is a Map? • A java Collection • NO INDICES • Each value is assigned a key • Each key is UNIQUE • Ask the map for a certain key, and he will give you the corresponding value

  3. Better mark it on my handy treasure map I think I’ll hide my treasure here x

  4. I can use the map to translate the X <key> into my treasure <value> x

  5. x

  6. Another Example ARRRRR MATEY Hmmm…what does that mean? I’ll ask my treasure map…. My first key is… AR My next key is… matey • A more common example <value> 1. annual return. 2. Arkansas 3. Army Regulation <value> 1. comrade2. chum 3. buddy

  7. Pirate Subtitles ARRRR matey Arkansas Chum

  8. Pirate Subtitles ARRRR matey Annual Return Comrade

  9. Map INTERFACE interface java.util.Map<K,V> • int size() • boolean containsKey(Object key) • V put(K key, V value) • V get(Object key) • V remove(Object key) • Set<K> keySet() public class HashMap implements Map

  10. An Example Map<String, String> phonebook = new HashMap<String, String>(); phonebook.put("Jack Sparrow", "555-5555"); phonebook.put("Ghostbusters", "555-1234"); phonebook.put("Ura Moron", "555-3333"); phonebook.put("Britney Spears", "1-800-HAS-BEEN"); phonebook.put("You Smell", "555-3321"); phonebook.put("Poopy Pants", "333-3321"); String person = "Britney Spears"; if(phonebook.containsKey(person)) System.out.println(person+"'s phone # is "+phonebook.get(person)); else System.out.println(person+" doesn't have a phone"); phonebook.put(person, "disconnected"); System.out.println(phonebook.get("Britney Spears")); System.out.println("Ura marries Poopy"); phonebook.remove("Ura Moron"); phonebook.put("Ura Pants", phonebook.get("Poopy Pants")); System.out.println(phonebook.get("Ura Pants")); System.out.println(phonebook.get("Poopy Pants")); Set<String> names = phonebook.keySet();

  11. An Example Map<String, String> phonebook = new HashMap<String, String>(); phonebook.put("Jack Sparrow", "555-5555"); phonebook.put("Ghostbusters", "555-1234"); phonebook.put("Ura Moron", "555-3333"); phonebook.put("Britney Spears", "1-800-HAS-BEEN"); phonebook.put("You Smell", "555-3321"); phonebook.put("Poopy Pants", "333-3321"); String person = "Britney Spears"; if(phonebook.containsKey(person)) System.out.println(person+"'s phone # is "+phonebook.get(person)); else System.out.println(person+" doesn't have a phone"); phonebook.put(person, "disconnected"); System.out.println(phonebook.get("Britney Spears")); System.out.println("Ura marries Poopy"); phonebook.remove("Ura Moron"); phonebook.put("Ura Pants", phonebook.get("Poopy Pants")); System.out.println(phonebook.get("Ura Pants")); System.out.println(phonebook.get("Poopy Pants")); Set<String> names = phonebook.keySet();

  12. More complicated example • Make a quick student class • Name • Grade • Gpa • Id # • Declare some students & add them to a map • KEY : id# • Value: Student object

More Related