Docsity

CSIS 110 Lecture 24: Hash Maps and Phone Book Exercise - Prof. Brian F. Hanks, Study notes of Javascript programming

Toggle sidebar
bg1
CSIS 110 - Lecture 24
Read: Section 5.5, 5.6, 5.8, and 5.9.
Quiz Results
Max: 25
Mean: 20
Median: 20.5
Distribution: <17.5: 2 17.5-19.5: 2 20-22: 5 22.5-24.5: 5 25: 1
Last Time: String methods startsWith, endsWith, and trim, tech-support system
Last time we looked at a tech support system. The first version was pretty stupid โ€“ it
always responded with the same message regardless of what our complaint was.
What did you do in the lab to make it a little less brain-dead? What do you think would
make it better? Somehow respond to the input in a way that makes more sense.
Hash Map
Before we go there, we need to look at another class in the Java library. This class is
called a HashMap.
Before I talk about this, think about a telephone directory or a dictionary. How do you
use them?
The idea is that you have a key (such as a person's name or a word) and there is a value
(such as a phone number or a definition) associated with the key. You use the key to look
up the value.
The HashMap allows us to do this. HashMap is a generic collection (what does "generic
collection" mean?)
class HashMap< K, V > {
public HashMap()
K โ€“ type of Key
V โ€“ type of Value
[Contrast with ArrayList<Element Type> ]
Note that we don't use an index with HashMap like we do with ArrayList โ€“ we use the
key. You don't look up an entry in the phone book with an index either.
pf3

Partial preview of the text

Download CSIS 110 Lecture 24: Hash Maps and Phone Book Exercise - Prof. Brian F. Hanks and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 - Lecture 24

Read: Section 5.5, 5.6, 5.8, and 5.9. Quiz Results Max: 25 Mean: 20 Median: 20. Distribution: <17.5: 2 17.5-19.5: 2 20-22: 5 22.5-24.5: 5 25: 1 Last Time: String methods startsWith, endsWith, and trim, tech-support system Last time we looked at a tech support system. The first version was pretty stupid โ€“ it always responded with the same message regardless of what our complaint was. What did you do in the lab to make it a little less brain-dead? What do you think would make it better? Somehow respond to the input in a way that makes more sense. Hash Map Before we go there, we need to look at another class in the Java library. This class is called a HashMap. Before I talk about this, think about a telephone directory or a dictionary. How do you use them? The idea is that you have a key (such as a person's name or a word) and there is a value (such as a phone number or a definition) associated with the key. You use the key to look up the value. The HashMap allows us to do this. HashMap is a generic collection (what does "generic collection" mean?) class HashMap< K, V > { public HashMap() K โ€“ type of Key V โ€“ type of Value [Contrast with ArrayList ] Note that we don't use an index with HashMap like we do with ArrayList โ€“ we use the key. You don't look up an entry in the phone book with an index either.

Examples A dictionary, where words (String) are mapped to definitions (String): HashMap< String, String > dictionary; A student body, where student IDs (Strings) are mapped to students (Student): HashMap< String, Student > studentBody; An address book, where people's names (String) are mapped to their addresses: public class Address { String street; String city; String state; String zipCode; } // Need a constructor and a toString method HashMap< String, Address > directory; HashMap methods: put( K key, V value ) โ€“ Add an entry (value) to the HashMap with the given key. [Show examples using above HashMaps โ€“ discuss FP types] V get( K key ) โ€“ get the value of the HashMap entry that has the given key. Returns null if there is no map entry for the given key. int size() โ€“ return the number of entries in the HashMap. boolean containsKey( K key ) โ€“ return true if the HashMap contains an entry with the given key. boolean containsValue( V value ) โ€“ return true if the HashMap contains an entry with the given value. Let's create the Address Book class. This class is used to associate people's names with their addresses. We will use objects of the Address class to store their addresses.

  1. Need to import java.util.HashMap;
  2. Define the HashMap. What type is the key? What type is the value?
  3. Create the HashMap in the AddressBook constructor
  4. Write the createEntry method. What are the types of its parameters?
  5. Write the lookupAddress method. What is the type of its parameter? What is its return type?