Maps

The Map interface is part of the Collections API, but unlike List and Set it does not extend the Collection interface. The primary reason for this is that each entry in a Map consists of two parts: a key and a value. This means that adding an element to a Map requires two values to be provided rather than one.

In some ways Maps are like HashSets: both data structures use indexed keys to facilitate fast lookup. The main difference is that with HashSets the key is implicitly generated from the hashKey method, whereas with Maps the key is explicitly provided, and therefore allows two otherwise unrelated objects to be associated with one another.

The two most common implementations of Map are HashMap and TreeMap. The main difference between these implementations is that TreeMaps sort elements based on their key, while HashMaps do not. HashMap should be your preferred option unless you have a specific reason for needing sorting, because it generally provides superior performance.

Consider a case where you want to quickly retrieve the Address of a particular person. You will start by defining a very simple Person class and adding hashCode and equals methods:

package addresses;

public class Person {

    String firstName;

    String lastName;

     

    public Person(String firstName, String lastName ) {

        this.firstName = firstName;

        this.lastName = lastName;

    }

    @Override

    public String toString() {

        return "Person [firstName=" + firstName +

            ", lastName=" + lastName + "]";

    }

 

    @Override

    public int hashCode() {

        final int prime = 31;

        int result = 1;

        result = prime * result +

           ((firstName == null) ? 0 : firstName.hashCode());

        result = prime * result +

           ((lastName == null) ? 0 : lastName.hashCode());

        return result;

    }

 

    @Override

    public boolean equals(Object obj) {

        if (this == obj)

            return true;

        if (obj == null)

             return false;

        if (getClass() != obj.getClass())

            return false;

        Person other = (Person) obj;

        if (firstName == null) {

            if (other.firstName != null)

                return false;

        } else if (!firstName.equals(other.firstName))

            return false;

        if (lastName == null) {

            if (other.lastName != null)

                return false;

        } else if (!lastName.equals(other.lastName))

            return false;

        return true;

    }      

}

//

Getters and setters have been omitted for brevity, and the fields have instead been defined using the default access modifier. You can choose to use private fields and getters and setters in your version.

Next, you will write a short program that associates Person and Address instances:

package addresses;

 

import java.util.HashMap;

import java.util.Map;

 

public class MapMain {

    public static void main(String[] args) {

        Address a1 = new Address(799, "E Dragham",

            "Tucson", 85705, "USA");

        Address a2 = new Address(200, "Main Street",

            "Phoenix", 85123, "USA");

        Address a3 = new Address(100, "Main Street",

            "Seattle", 98104, "USA");

           

        Person p1 = new Person("Dane", "Cameron");

        Person p2 = new Person("James", "Smith");

        Person p3 = new Person("Keith", "Rogers");

        Person p4 = new Person("Owen", "Heart");

           

        Map people = new HashMap();

        people.put(p1, a3);

        people.put(p2, a1);

        people.put(p3, a2);

        people.put(p4, a1);

           

        System.out.println("Dane lives at " + people.get(p1));

        people.put(p1, a2);

        System.out.println("Now Dane lives at " + people.get(p1));

    }

}

This produces the following output:

Dane lives at 100 Main Street, Seattle, USA

Now Dane lives at 200 Main Street, Phoenix, USA

Maps enforce a number of rules:

  • >Each key can only occur once in the Map, if you put a key that already exists, the value associated with this will be updated. This is why the Address for Dane changed in the example program.

  • null values can be used for both the key and the value, but there can only be a single entry with a null key.

Maps generally provide extremely good performance when accessing values based on their key. This is, however, dependent on the object being used as the key providing a hashCode implementation that produced well-distributed values, because, as with Sets, lookups are optimized through the use of the hashCode.

A Software Engineer Learns Java and Object Orientated Programming
titlepage.xhtml
part0000_split_000.html
part0000_split_001.html
part0000_split_002.html
part0000_split_003.html
part0000_split_004.html
part0000_split_005.html
part0000_split_006.html
part0000_split_007.html
part0000_split_008.html
part0000_split_009.html
part0000_split_010.html
part0000_split_011.html
part0000_split_012.html
part0000_split_013.html
part0000_split_014.html
part0000_split_015.html
part0000_split_016.html
part0000_split_017.html
part0000_split_018.html
part0000_split_019.html
part0000_split_020.html
part0000_split_021.html
part0000_split_022.html
part0000_split_023.html
part0000_split_024.html
part0000_split_025.html
part0000_split_026.html
part0000_split_027.html
part0000_split_028.html
part0000_split_029.html
part0000_split_030.html
part0000_split_031.html
part0000_split_032.html
part0000_split_033.html
part0000_split_034.html
part0000_split_035.html
part0000_split_036.html
part0000_split_037.html
part0000_split_038.html
part0000_split_039.html
part0000_split_040.html
part0000_split_041.html
part0000_split_042.html
part0000_split_043.html
part0000_split_044.html
part0000_split_045.html
part0000_split_046.html
part0000_split_047.html
part0000_split_048.html
part0000_split_049.html
part0000_split_050.html
part0000_split_051.html
part0000_split_052.html
part0000_split_053.html
part0000_split_054.html
part0000_split_055.html
part0000_split_056.html
part0000_split_057.html
part0000_split_058.html
part0000_split_059.html
part0000_split_060.html
part0000_split_061.html
part0000_split_062.html
part0000_split_063.html
part0000_split_064.html
part0000_split_065.html
part0000_split_066.html
part0000_split_067.html
part0000_split_068.html
part0000_split_069.html
part0000_split_070.html
part0000_split_071.html
part0000_split_072.html
part0000_split_073.html
part0000_split_074.html
part0000_split_075.html
part0000_split_076.html
part0000_split_077.html
part0000_split_078.html
part0000_split_079.html
part0000_split_080.html
part0000_split_081.html
part0000_split_082.html
part0000_split_083.html
part0000_split_084.html
part0000_split_085.html
part0000_split_086.html
part0000_split_087.html
part0000_split_088.html
part0000_split_089.html
part0000_split_090.html
part0000_split_091.html
part0000_split_092.html
part0000_split_093.html
part0000_split_094.html
part0000_split_095.html
part0000_split_096.html
part0000_split_097.html
part0000_split_098.html
part0000_split_099.html
part0000_split_100.html
part0000_split_101.html
part0000_split_102.html
part0000_split_103.html
part0000_split_104.html
part0000_split_105.html
part0000_split_106.html
part0000_split_107.html
part0000_split_108.html
part0000_split_109.html
part0000_split_110.html
part0000_split_111.html
part0000_split_112.html
part0000_split_113.html
part0000_split_114.html
part0000_split_115.html
part0000_split_116.html
part0000_split_117.html
part0000_split_118.html
part0000_split_119.html
part0000_split_120.html
part0000_split_121.html
part0000_split_122.html
part0000_split_123.html
part0000_split_124.html
part0000_split_125.html
part0000_split_126.html
part0000_split_127.html
part0000_split_128.html
part0000_split_129.html
part0000_split_130.html
part0000_split_131.html
part0000_split_132.html
part0000_split_133.html
part0000_split_134.html
part0000_split_135.html
part0000_split_136.html
part0000_split_137.html
part0000_split_138.html
part0000_split_139.html