Provide suitable hashCode and equals methods for the Pair class of Section 17.2 and implement a HashMap class, using a HashSet<Pair<K, V>>.

Complete the following files:

HashMap.java

import java.util.HashSet; import java.util.Collection; import java.util.Set; public class HashMap<K, V> { private HashSet<Pair<K, V>> set; public HashMap() { . . . } public void put(K key, V value) { . . . } public V get(K key) { . . . } public V remove(K key) { . . . } public Set<K> keySet() { . . . } public int size() { return set.size(); } }

Pair.java

public class Pair<T, S> { private T first; private S second; public Pair(T firstElement, S secondElement) { first = firstElement; second = secondElement; } public T getFirst() { return first; } public S getSecond() { return second; } public boolean equals(Object otherObject) { . . . } public int hashCode() { . . . } }

Use the following file:

MapTester.java

import java.awt.Color;
import java.util.Map;
import java.util.Set;

/**
   This program tests a map that maps names to colors.
*/
public class MapTester
{
   public static void main(String[] args)
   {      
      HashMap<String, Color> favoriteColors =
            new HashMap<String, Color>();
      favoriteColors.put("Juliet", Color.PINK);
      favoriteColors.put("Romeo", Color.GREEN);
      favoriteColors.put("Adam", Color.BLUE);
      favoriteColors.put("Eve", Color.PINK);
      favoriteColors.put("Romeo", Color.WHITE);
      favoriteColors.remove("Juliet");
      System.out.println(favoriteColors.get("Romeo"));
      System.out.println("Expected: java.awt.Color[r=255,g=255,b=255]");
      System.out.println(favoriteColors.get("Juliet"));
      System.out.println("Expected: null");
      System.out.println(favoriteColors.size());
      System.out.println("Expected: 3");
      Set<String> keys = favoriteColors.keySet();
      System.out.println(keys.size());
      System.out.println("Expected: 3");
   }
}