structure5
Class Association<K,V>

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

public class Association<K,V>
extends java.lang.Object
implements java.util.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()+ ".");
      }
 }
 


Field Summary
protected  K theKey
          The immutable key.
protected  V theValue
          The mutable value.
 
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(java.lang.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.
 java.lang.String toString()
          Standard string representation of an association.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

theKey

protected K theKey
The immutable key. An arbitrary object.


theValue

protected V theValue
The mutable value. An arbitrary object.

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.
Precondition:
key is non-null
Postcondition:
constructs a key-value pair

Association

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

Parameters:
key - A non-null key value.
Precondition:
key is non-null
Postcondition:
constructs a key-value pair; value is null
Method Detail

equals

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

Specified by:
equals in interface java.util.Map.Entry<K,V>
Overrides:
equals in class java.lang.Object
Parameters:
other - Another association.
Returns:
true iff the keys are equal.
Precondition:
other is non-null Association
Postcondition:
returns true iff the keys are equal

hashCode

public int hashCode()
Standard hashcode function.

Specified by:
hashCode in interface java.util.Map.Entry<K,V>
Overrides:
hashCode in class java.lang.Object
Returns:
A hash code for association.
See Also:
Hashtable
Postcondition:
return hash code association with this association

getValue

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

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

getKey

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

Specified by:
getKey in interface java.util.Map.Entry<K,V>
Returns:
Key of the key-value pair.
Postcondition:
returns key from association

setValue

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

Specified by:
setValue in interface java.util.Map.Entry<K,V>
Parameters:
value - The new value.
Postcondition:
sets association's value to value

toString

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

Overrides:
toString in class java.lang.Object
Returns:
String representing key-value pair.
Postcondition:
returns string representation