/** * Generic class representing an association of a key with a value * @author Duane Bailey * @version 2007 * @param Type of keys * @param Type of value * Class takes types as parameters! */ public class Association { private K theKey; // the key of the key-value pair private V theValue; // the value of the key-value pair /* * Usage example: Association personAttribute = * new Association("Age",34); */ /** * @param key * is non-null value used to find the associated value * @param value * is value associated with the key * post: constructs a key-value pair */ public Association(K key, V value) { theKey = key; theValue = value; } /** * @return value from association */ public V getValue() { return theValue; } /** * @return returns key from association */ public K getKey() { return theKey; } /** * @param value * is new value associated with current key in association * @return value before update * post: current key is now associated with new value */ public V setValue(V value) { V oldValue = theValue; theValue = value; return oldValue; } public static void main(String[] args) { Association nameAgePair = new Association("Shezad",24); // Can't use primitive type as generic parameter, so must // use Integer instead of int. Value 24 is "autoboxed" into // Integer value System.out.println(nameAgePair.getKey()+" is now "+nameAgePair.getValue() +" years old"); } }