How do I list print out all the keys currently stored in my HashMap mapping people to their addresses.
如何列出打印出当前存储在我的HashMap中的所有键,将人们映射到他们的地址。
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.
这是我到目前为止所尝试的,并且知道我需要使用keySet方法,但我不确定如何应用它。
4 个解决方案
#1
1
You can use either of the following
1) Use the keySet
您可以使用以下任一项1)使用keySet
Set keys = addressBook.keySet();
for (Iterator i = keys.iterator(); i.hasNext();) {
String key = (String) i.next();
String value = (String) addressBook.get(key);
System.out.println("key=" + key + ", value=" + value);
}
or use the Hashmap values method which returns a Collection view of the values contained in the map
或使用Hashmap values方法,该方法返回地图中包含的值的Collection视图
for (String value : addressBook.values()) {
System.out.println("value=" + value);
}
or directly print the collection returned by values using Arrays
或使用Arrays直接打印由值返回的集合
System.out.println("addressBook.values =" + Arrays.asList(addressBook.values()));
#2
2
for (Map.Entry<String, String> entry : addressBook .entrySet()) {
System.out.println("key=" + entry.getKey() + ", value=" + entry.getValue());
}
or
Set<String> keys = addressBook.keySet(); // for key set
Iterator iterator=keys.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
#3
1
A hashmap has a method keySet which will return a Set of keys
hashmap有一个方法keySet,它将返回一组键
http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#keySet()
In your case
在你的情况下
Set<String> keys = addressBook.keySet();
for (String key : keys) {
System.out.println (key);
}
#4
0
You need to iterate over all the keys of your phoneBook
and get their corresponding address from the addressBook
.
您需要遍历phoneBook的所有键并从addressBook获取相应的地址。
for (String key : phoneBook.keySet()) {
System.out.println("name=" + key + ", address=" + addressBook.get(key));
}
#1
1
You can use either of the following
1) Use the keySet
您可以使用以下任一项1)使用keySet
Set keys = addressBook.keySet();
for (Iterator i = keys.iterator(); i.hasNext();) {
String key = (String) i.next();
String value = (String) addressBook.get(key);
System.out.println("key=" + key + ", value=" + value);
}
or use the Hashmap values method which returns a Collection view of the values contained in the map
或使用Hashmap values方法,该方法返回地图中包含的值的Collection视图
for (String value : addressBook.values()) {
System.out.println("value=" + value);
}
or directly print the collection returned by values using Arrays
或使用Arrays直接打印由值返回的集合
System.out.println("addressBook.values =" + Arrays.asList(addressBook.values()));
#2
2
for (Map.Entry<String, String> entry : addressBook .entrySet()) {
System.out.println("key=" + entry.getKey() + ", value=" + entry.getValue());
}
or
Set<String> keys = addressBook.keySet(); // for key set
Iterator iterator=keys.iterator();
while(iterator.hasNext()) {
System.out.println(iterator.next());
}
#3
1
A hashmap has a method keySet which will return a Set of keys
hashmap有一个方法keySet,它将返回一组键
http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#keySet()
In your case
在你的情况下
Set<String> keys = addressBook.keySet();
for (String key : keys) {
System.out.println (key);
}
#4
0
You need to iterate over all the keys of your phoneBook
and get their corresponding address from the addressBook
.
您需要遍历phoneBook的所有键并从addressBook获取相应的地址。
for (String key : phoneBook.keySet()) {
System.out.println("name=" + key + ", address=" + addressBook.get(key));
}