Question
Programming Problem 18.12 hashCode and equals methods for the Pair class with LabRat Provide suitable hashCode and equals methods for the Pair class of Section
Programming Problem 18.12 hashCode and equals methods for the Pair class with LabRat
Provide suitable hashCode and equals methods for the Pair class of Section 17.2 and implement a HashMap class, using a HashSet
Complete the following classes in your solution:
public class Pair{ 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() { . . . } }
import java.util.HashSet; import java.util.Collection; import java.util.Set; public class HashMap
Use the following class as your tester class:
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) { HashMapfavoriteColors = new HashMap (); 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 keys = favoriteColors.keySet(); System.out.println(keys.size()); System.out.println("Expected: 3"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started