© 1998-2002 McGraw-Hill

structure
Class Association

java.lang.Object
  |
  +--structure.Association
All Implemented Interfaces:
Map.Entry
Direct Known Subclasses:
ComparableAssociation

public class Association
extends Object
implements Map.Entry

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  Object theKey
          The immutable key.
protected  Object theValue
          The mutable value.
 
Constructor Summary
Association(Object key)
          Constructs a pair from a key; value is null.
Association(Object key, Object value)
          Constructs a pair from a key and value.
 
Method Summary
 boolean equals(Object other)
          Standard comparison function.
 Object getKey()
          Fetch key from association.
 Object getValue()
          Fetch value from association.
 int hashCode()
          Standard hashcode function.
 Object setValue(Object value)
          Sets the value of the key-value pair.
 String toString()
          Standard string representation of an association.
 
Methods inherited from class java.lang.Object
, clone, finalize, getClass, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

theKey

protected Object theKey
The immutable key. An arbitrary object.

theValue

protected Object theValue
The mutable value. An arbitrary object.
Constructor Detail

Association

public Association(Object key,
                   Object value)
Constructs a pair from a key and value.
Parameters:
key - A non-null object.
value - A (possibly null) object.

Association

public Association(Object 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
Overrides:
equals in class Object
Parameters:
other - Another association.
Precondition:
other is non-null Association
Postcondition:
returns true iff the keys are equal
Returns:
true iff the keys are equal.

hashCode

public int hashCode()
Standard hashcode function.
Specified by:
hashCode in interface Map.Entry
Overrides:
hashCode in class Object
Postcondition:
return hash code association with this association
Returns:
A hash code for association.
See Also:
Hashtable

getValue

public Object getValue()
Fetch value from association. May return null.
Specified by:
getValue in interface Map.Entry
Postcondition:
returns value from association
Returns:
The value field of the association.

getKey

public Object getKey()
Fetch key from association. Should not return null.
Specified by:
getKey in interface Map.Entry
Postcondition:
returns key from association
Returns:
Key of the key-value pair.

setValue

public Object setValue(Object value)
Sets the value of the key-value pair.
Specified by:
setValue in interface Map.Entry
Parameters:
value - The new value.
Postcondition:
sets association's value to value

toString

public String toString()
Standard string representation of an association.
Overrides:
toString in class Object
Postcondition:
returns string representation
Returns:
String representing key-value pair.

© 1998-2002 McGraw-Hill