Skip to main content
ICT
Lesson AB28 - Sets and Maps
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

E. Maps page 7 of 11

  1. A map maintains a correspondence between elements of two sets of objects. The objects in one set are thought to be “keys,” and the objects in the other set are thought to be “values.” A map cannot contain duplicate keys, which means that each key maps to only one value. Different keys, however, can map to the same value.

  2. An example of a map is an email directory: the keys in the map are the email addresses and the values are the names in the address book. Each email address in the directory maps to one name. Duplicate keys are not allowed, so the keys in a map form a set. Note that this does not preclude two different keys mapping to the same value, just as two email addresses can belong to one name. Like the elements of a Set, the keys in a Map should be immutable.

  3. The Map interface allows the following operations:

    • insert a key/value pair into the map
    • retrieve any value, given its key
    • test if a given key is in the map
    • view the elements in the map
    • iterate over the mapping elements, using Iterator

  4. In Java, this functionality is formalized by the java.util.Map interface. A few of Map’s commonly used methods that are included as part of the AP Subset are shown in Figure 28.2 below.

    1. The put method adds a key/value association to the map. If the key was previously associated with a different value, the old association is broken and put returns the value previously associated with the key. If the key had no prior association with a value, put returns null.

    2. The get method returns the value associated with a given key or null if the key is not associated with any value.

    3. The containsKey method returns true if a given key is associated with a value and false otherwise.

  5. Figure 28.2 below lists the methods of the Map interface that are a part of the AP subset.

// Adds the key-value pair to this map. Returns the
// previous value associated with specified key, or
// null if there was no mapping for key.
Object put(Object key, Object value);

// Returns the value to which the specified key is
// mapped, or null if the map contains no mapping
// for this key.
Object get(Object key);

// Removes the mapping for this key from this map
// if present. Returns the value to which the map
// previously associated with the key, or null if
// the map contained no mapping for this key. Note
// that a return value of null may also indicate
// that this key was previously mapped to null
Object remove(Object key);

// Returns true if this map contains a mapping for
// the specified key, false otherwise.
boolean containsKey(Object key);

// Returns the number of key-value pairs in the map.
int size();

// Returns a set containing the keys in this map.
Set keySet()


Figure 28.2 - Methods of the Map interface included in the AP Subset

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.