In: Computer Science
CS 209 Data Structure
5. Consider the Pair class covered in class:
class Pair
{
public A first;
public B second;
public Pair(A a, B b)
{
first = a;
second = b;
}
public void setFirst(A a)
{
first = a;
}
public A getFirst()
{
return first;
}
public void setSecond(B b)
{
second = b;
}
public B getSecond()
{
return second;
}
}
a. Create a toString() method inside the Pair class.
b. Create an equals() method inside the Pair class.
c. Create a hashCode() method inside the Pair class.
d. Create a HashSet of Pair and insert a few elements including duplicates.
Verify that the HashSet removes all the duplicates.
A.java:
import java.util.Objects; public class A { String str; /** * constructor with single parameter * @param str */ A(String str) { this.str = str; } /** * convert current object to string * @return */ public String toString() { return str; } /** * function to equality check * @param o * @return */ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; A a = (A) o; return Objects.equals(str, a.str); } public int hashCode() { return Objects.hash(str); } }
B.java:
import java.util.Objects; public class B { String str; /** * constructor with single parameter * @param str */ B(String str) { this.str = str; } /** * convert current object to string * @return */ public String toString() { return str; } /** * function to equality check * @param o * @return */ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; B b = (B) o; return Objects.equals(str, b.str); } public int hashCode() { return Objects.hash(str); } }
Pair.java:
import java.util.Objects; /** * class pair */ class Pair { public A first; public B second; /** * constructor with two parameter * @param a * @param b */ public Pair(A a, B b) { first = a; second = b; } /** * setter method for first * @param a */ public void setFirst(A a) { first = a; } /** * getter method for first * @return */ public A getFirst() { return first; } /** * setter method for second * @param b */ public void setSecond(B b) { second = b; } /** * getter method for second * @return */ public B getSecond() { return second; } /** * function to get string * @return */ public String toString() { return "Pair{" + "first=" + first + ", second=" + second + '}'; } /** * check whether two pairs are equal or not * @param o * @return */ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; return Objects.equals(first, pair.first) && Objects.equals(second, pair.second); } /** * function to get hash code of current object * @return */ public int hashCode() { return Objects.hash(first, second); } }
Driver.java:
import java.util.HashSet; public class Driver { public static void main(String[] args) { // create new hashset of pair HashSet<Pair> hashSet = new HashSet<>(); // add new pair hashSet.add(new Pair(new A("A0"), new B("B0"))); // add new pair hashSet.add(new Pair(new A("A1"), new B("B1"))); // add duplicate hashSet.add(new Pair(new A("A1"), new B("B1"))); // add new pair hashSet.add(new Pair(new A("A2"), new B("B2"))); // addd duplicate hashSet.add(new Pair(new A("A2"), new B("B2"))); // print hash set for (Pair p: hashSet) { System.out.println(p); } } }
Output: