130 likes | 330 Views
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.
E N D
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
I can use the map to translate the X <key> into my treasure <value> x
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
Pirate Subtitles ARRRR matey Arkansas Chum
Pirate Subtitles ARRRR matey Annual Return Comrade
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
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();
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();
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