structure5
Class Association<K,V>

java.lang.Object
  extended by structure5.Association<K,V>
All Implemented Interfaces:
Map.Entry<K,V>
Direct Known Subclasses:
ComparableAssociation, HashAssociation

public class Association<K,V>
extends Object
implements Map.Entry<K,V>

A class implementing a key-value pair. This class associates an immutable key with a mutable value. Used in many other structures.

Example Usage:

To store the number of classes a student has taken from five different professors and to output this information, we could use the following.

 public static void main(String[] argv){
        //store the number of classes taken by the student in an array of associations
        Association [] classesTaken = new Association[5];
        classesTaken[0] = new Association("Andrea", new Integer(5));
        classesTaken[1] = new Association("Barbara", new Integer(1));
        classesTaken[2] = new Association("Bill", new Integer(3));
        classesTaken[3] = new Association("Duane", new Integer(2));
        classesTaken[4] = new Association("Tom", new Integer(1));

        //print out each item in the array
        for (int i = 0; i< classesTaken.length; i++){
            System.out.println("This Student has taken " + classesTaken[i].getValue() +
                               " classes from " + classesTaken[i].getKey()+ ".");
        }
 }
 


Constructor Summary
Association(K key)
          Constructs a pair from a key; value is null.
Association(K key, V value)
          Constructs a pair from a key and value.
 
Method Summary
 boolean equals(Object other)
          Standard comparison function.
 K getKey()
          Fetch key from association.
 V getValue()
          Fetch value from association.
 int hashCode()
          Standard hashcode function.
 V setValue(V value)
          Sets the value of the key-value pair.
 String toString()
          Standard string representation of an association.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Association

public Association(K key,
                   V value)
Constructs a pair from a key and value.

Parameters:
key - A non-null object.
value - A (possibly null) object.

Association

public Association(K key)
Constructs a pair from a key; value is null.

Parameters:
key - A non-null key value.
Method Detail

equals

public boolean equals(Object other)
Standard comparison function. Comparison based on keys only.

Specified by:
equals in interface Map.Entry<K,V>
Overrides:
equals in class Object
Parameters:
other - Another association.
Returns:
true iff the keys are equal.

hashCode

public int hashCode()
Standard hashcode function.

Specified by:
hashCode in interface Map.Entry<K,V>
Overrides:
hashCode in class Object
Returns:
A hash code for association.
See Also:
Hashtable

getValue

public V getValue()
Fetch value from association. May return null.

Specified by:
getValue in interface Map.Entry<K,V>
Returns:
The value field of the association.

getKey

public K getKey()
Fetch key from association. Should not return null.

Specified by:
getKey in interface Map.Entry<K,V>
Returns:
Key of the key-value pair.

setValue

public V setValue(V value)
Sets the value of the key-value pair.

Specified by:
setValue in interface Map.Entry<K,V>
Parameters:
value - The new value.

toString

public String toString()
Standard string representation of an association.

Overrides:
toString in class Object
Returns:
String representing key-value pair.