To continue learning about HashMap
:
- How to add and get values by key (review)
- How to use more advanced methods (
size
,remove
,clear
,isEmpty
)
Introducing this week's homework...
Let's look at how much simpler "The Twelve Days of Christmas" assignment can be with a HashMap
.
Definition: A map is a collection that associates keys with values.
Exercise: Create a class
WordTracker
; importHashMap
; and create a newHashMap
namedwords
. Its keys should be of typeString
and its values of typeInteger
.
Exercise: Prompt the user for some words; continue reading in words until the user inputs "stop". When calling
put()
, just pass 1 as the second parameter. Here's example program:Please give me some words: latte coffee mocha stop Thanks!
Just like with an ArrayList
, you can get the size of a HashMap
by calling the method size
.
Exercise: Modify your program to tell the user how many words they input.
Please give me some words: latte coffee stop Thanks! You have given me 2 word(s)!
Call remove(key)
to remove the key
parameter from the HashMap
. For example, to remove "latte" from the previous exercise, we would write:
words.remove("latte");
Exercise: Modify your program so that if the user types
rm
, every subsequent word the user types removes it from theHashMap
. If the user typesadd
, the program switches back to adding. Use a booleanaddMode
to track which mode the program is in. Initialize it outside of your loop.Please give me some words: latte coffee rm coffee stop Thanks! You have given me 1 word(s)!
Here's another program run:
Please give me some words: foo bar rm bar add qux stop Thanks! You have input 2 words.
Call clear()
to remove all the elements from the HashMap
.
Exercise: Modify your program so that if the user types "clr", the tracked words are cleared.
Please give me some words: foo bar clr stop Thanks! You have input 0 words.
Call isEmpty()
to return a boolean
indicating whether or not the HashMap
is empty. Notice it is just syntactic sugar for...
words.size() == 0;
Exercise: Modify your program to check if
words
is empty at the end of the program.Please give me some words: stop Thanks! You have input 0 words. Words is empty.
There are many ways to iterate over a HashMap
. We're going to learn just one:
for (String word : words.keySet()) {
}
We'll look at the Set
data structure on Thursday; for now, just know that calling keySet()
returns an iterable collection and that you can iterate over it like above.
Exercise: Modify your program to print the counts of each word. For example:
Please give me some words: banana orange stop Thanks! You have input 2 words. Here they are: orange: 1 banana: 1
Final exercise!
Exercise: Modify your program to keep track of multiple inputs of the same word. For example:
Please give me some words: banana banana banana orange stop Thanks! You have input 2 words. Here they are: orange: 1 banana: 3
You've written a complex program! This kind of input should work for you now:
Please give me some words:
banana
orange
rm
orange
add
water
sugar
juice
juice
juice
juice
stop
Thanks! You have input 4 words.
Here they are:
water: 1
banana: 1
juice: 4
sugar: 1
We'll be using NYC Open Data again to build a HashMap
of restaurant data. Use FileParser
to get an ArrayList
representing the lines in the file and follow this stencil file.
Here's a simple program execution:
Name a restaurant:
MORRIS PARK BAKE SHOP
A
Modify your
HashMap
ofString
s to hold anArrayList
ofString
s. When the user inputs a restaurant name, print all the relevant facts that you have.