I have a project about car registrations. I have a class named RegNo which implements the comparable interface. It should contain the equals and hashCode functions overridden as I am using this in hashmap. Kindly help me get along with this. The class code is as follows:
package main;
public class RegNo implements Comparable<RegNo> {
private final String regNo;
public RegNo(String regNo)
{
this.regNo = regNo;
}
/*
* implementing the compareTO method which is defined
* in Comparable class as an abstract method
* the method returns 0 if both the registration numbers being compared are equal
* it returns 1 if the regNo of the object calling this method is lexicographically greater than the parameter
* and the method returns a -1, if the regNo of the parameter is greater than the object calling the method
* */
@Override
public int compareTo(RegNo reg) {
// TODO Auto-generated method stub
if(this.regNo == reg.regNo) //both the registration numbers are equal
return 0;
else if(this.regNo.compareTo(reg.regNo) > 0)
return 1;
else
return -1;
}
}