@SuppressWarnings("unchecked") // needed because of casting used in the equals method public class KeyValuePair // demonstrate a Generic class with different types of instance data { private K key; // key stores the attribute name, e.g., "age" private V value; // value stores the attribute value, e.g., a person's age public KeyValuePair() // no-arg, kind of worthless { key=null; value=null; } public KeyValuePair(K key) // we know the key but not the value (ye) { this.key=key; value=null; } public KeyValuePair(K key, V value) // we know both the key and value { this.key=key; this.value=value; } public K getKey() // accessors and mutators { return key; } public V getValue() { return value; } public void setKey(K key) { this.key=key; } public void setValue(V value) { this.value=value; } // implement the equal method to see if two KeyValuePairs are equal, we discuss this generic method later public static boolean equal(KeyValuePair p1, KeyValuePair p2) { K key1=(K)p1.getKey(); // note the casting, get the two KeyValuePair's keys K key2=(K)p2.getKey(); V value1=(V)p1.getValue(); // get the values V value2=(V)p2.getValue(); // if both keys are equal and both values are equal then they are equal if(key1.compareTo(key2)==0&&value1.compareTo(value2)==0) return true; else return false; } }