The Map operation

The ability to map objects to other objects is a great way to solve programming problems. For example, consider a program that checks for randomness in Java’s Random class. In an ideal state, Random can produce an ideal number distribution, but to test it, it is necessary to generate a large number of Random numbers and count the numbers falling into various ranges. Map can easily solve this problem. In this case, the key is a number generated by Random, and the value is the number of occurrences of that number.

package p10;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class Statistics {
    public static void main(String[] args) {
        Random rand = new Random(47);
        Map<Integer,Integer> map = new HashMap<>();
        for(int i = 0; i <10000; i++){// Produce a number between 0 and 20
            int r = rand.nextInt(20);
            Integer freq = map.get(r);
            map.put(r,freq == null?1:freq+1);
        }
        System.out.println(map);
        / * * * {1 = 0 = 481, 502, 2 = 489, 3 = 508, 4 = 481, 5 = 503, 6 = 519, 7 = 471, 8 = 468, 9 = 549, * 10 = 513, 11 = 531, 12 = 521, 13 = 506, 14 = 477, 15=497, 16=533, 17=509, 18=478, 19=464} */}}Copy the code

In main(), the automatic wrapping mechanism converts randomly generated ints into Integer references that can be used by a HashMap (no primitive containers). If the key is not in the container, the get0 method returns NULL (which means the number was found for the first time). Otherwise, the get() method produces the Integer value associated with the key, which is then incremented (automatic wrapping simplifies the expression again, but wrapping and unpacking of the Integer does occur).

The following example allows you to find Pet using a String description, and it shows how you can test a Map by using containsKey() and containsValue() to see if it contains a key or a value.

package p10;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

public class PetMap {
    public static void main(String[] args) {
        Map<String,Pet> 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));
        /** * {My Dog=Dog Ginger, My Cat=Cat Molly, My Hamster=Hamster Bosco} * Dog Ginger * true * true */}}Copy the code

Maps, like arrays and other collections, can easily be extended to multiple dimensions, and all we need to do is set others to Maps (the values of these maps can make other containers, or even other maps). Therefore, we can easily combine containers to quickly generate powerful data structures. For example, suppose you are tracking people with multiple pets and all you need is a Map<Person,List>