How do I list print out all the keys currently stored in my HashMap mapping people to their addresses.
import java.util.HashMap;
public class MapTester
{
private HashMap<String, String> addressBook = new HashMap<String, String> ();
private String name;
private String address;
public MapTester()
{
addressBook.put("Zoe", "9 Applebury Street");
addressBook.put("Mum", "72 Cherry Tree Drive");
addressBook.put("Dad", "6 Windsor Avenue");
}
/**
* Input the name and address
*/
public void enterContact(String name, String address)
{
addressBook.put(name, address);
}
/**
* Lookup a contact's name from their address.
*/
public String lookupNumber(String name)
{
name = name;
return addressBook.get(name);
}
public void keySet()
{
for (String contacts : addressBook)
{
System.out.println(contacts);
}
}
}
This is what i've attempted so far and know I need to use the keySet method but am unsure how to apply it.
You can use either of the following
1) Use the keySet
or use the Hashmap values method which returns a Collection view of the values contained in the map
or directly print the collection returned by values using Arrays