

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


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
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.