将对象映射到其他对象的能力是一种解决编程问题的杀手锏。例如,考虑一个程序,它将用来检查java的Random类的随机性。理想状态下,Random可以将产生理想的数字分布,但要想测试它,则需要生成大量的随机数,并对落入各种不同范围的数字进行计数。Map可以很容易地解决该问题。
import java.util.*;
public class Statistics {
public static void main(String[] args) {
Random rand = new Random(47);
Map m = new HashMap();
for(int i = 0; i < 10000; i++) {
int r = rand.nextInt(20);
Integer freq = m.get(r);
m.put(r, freq == null ? 1 : freq + 1);
}
System.out.println(m);
}
} /* Output:
{15=497, 4=481, 19=464, 8=468, 11=531, 16=533, 18=478, 3=508, 7=471, 12=521, 17=509, 2=489, 13=506, 9=549, 6=519, 1=502, 14=477, 10=513, 5=503, 0=481}
*///:~
下面的示例允许你是用一个String描述来查找Pet对象,并且展示通过containsKey()和containsValue()来测试一个Map,以便查看它是否包含某个键或某个值。
import java.util.*;
import pets.Cat;
import pets.Dog;
import pets.Hamster;
import pets.Pet;
public class PetMap {
public static void main(String[] args) {
Map petMap = new HashMap();
petMap.put("My Cat", new Cat("Molly"));
petMap.put("My Dog", new Dog("Ginger"));
petMap.put("My Hamster", new Hamster("Bosco"));
System.out.println(petMap);
Pet dog = petMap.get("My Dog");
System.out.println(dog);
System.out.println(petMap.containsKey("My Dog"));
System.out.println(petMap.containsValue(dog));
}
} /* Output:
{My Cat=Cat Molly, My Hamster=Hamster Bosco, My Dog=Dog Ginger}
Dog Ginger
true
true
*///:~
Map与数组和其他的Collection一样,可以很容易地扩展到多维。因此,我们能够很容易地将容器组合起来从而快速地生成强大的数据结构。例如,假设我们调查拥有多个宠物的人,你所需的只是一个Map
import java.util.*;
import pets.Cat;
import pets.Cymric;
import pets.Dog;
import pets.Mutt;
import pets.Person;
import pets.Pet;
import pets.Pug;
import pets.Rat;
public class MapOfList {
public static Map>
petPeople = new HashMap>();
static {
petPeople.put(new Person("Dawn"),
Arrays.asList(new Cymric("Molly"),new Mutt("Spot")));
petPeople.put(new Person("Kate"),
Arrays.asList(new Cat("Shackleton"),
new Cat("Elsie May"), new Dog("Margrett")));
petPeople.put(new Person("Marilyn"),
Arrays.asList(
new Pug("Louie aka Louis Snorkelstein Dupree"),
new Cat("Stanford aka Stinky el Negro"),
new Cat("Pinkola")));
petPeople.put(new Person("Luke"),
Arrays.asList(new Rat("Fuzzy"), new Rat("Fizzy")));
petPeople.put(new Person("Isaac"),
Arrays.asList(new Rat("Freckly")));
}
public static void main(String[] args) {
System.out.println("People: " + petPeople.keySet());
System.out.println("Pets: " + petPeople.values());
for(Person person : petPeople.keySet()) {
System.out.println(person + " has:");
for(Pet pet : petPeople.get(person))
System.out.println(" " + pet);
}
}
} /* Output:
People: [Person Luke, Person Marilyn, Person Isaac, Person Dawn, Person Kate]
Pets: [[Rat Fuzzy, Rat Fizzy], [Pug Louie aka Louis Snorkelstein Dupree, Cat Stanford aka Stinky el Negro, Cat Pinkola], [Rat Freckly], [Cymric Molly, Mutt Spot], [Cat Shackleton, Cat Elsie May, Dog Margrett]]
Person Luke has:
Rat Fuzzy
Rat Fizzy
Person Marilyn has:
Pug Louie aka Louis Snorkelstein Dupree
Cat Stanford aka Stinky el Negro
Cat Pinkola
Person Isaac has:
Rat Freckly
Person Dawn has:
Cymric Molly
Mutt Spot
Person Kate has:
Cat Shackleton
Cat Elsie May
Dog Margrett
*///:~
Map可以返回它的键Set,它的值Collection,或者它的键值对的Set。keySet()方法产生了由在petPeople中的所有键组成的Set,它在foreach语句中被用来迭代遍历该Map.
特别需要说明的一个地方,Map内部提供了一个Entry
import java.util.*;
public class EnvironmentVariables {
public static void main(String[] args) {
for(Map.Entry entry: System.getenv().entrySet()) {
System.out.println(entry.getKey() + ": " +
entry.getValue());
}
}
}