0

I'm working with hashCode for the first time and not sure how to check if two objects are equal. This is what I have so far.

/** Represents a City */
class City {
/** Decimal format to print leading zeros in zip code */
static DecimalFormat zipFormat = new DecimalFormat("00000");

    int zip;
    String name;
    String state;
    double longitude;
    double latitude;


    /** The full constructor */
    public City (int zip, String name, String state, 
            double longitude, double latitude) {
            this.zip   = zip;
            this.name  = name;
            this.state = state;
            this.longitude = longitude;
            this.latitude  = latitude;
}

/** to make sure the two cities have the same name, state, zip code,
    * and the same latitude and longitude */
    public boolean equals(Object obj){
        if (obj == null)
            return false;
            City temp = (City)obj;

            System.out.println(City.equals(obj));
    }
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332

2 Answers2

0

Assuming equality in your case means all properties are same in your equals method add

City temp = (City)obj;
return this.zip == temp.zip &&
       this.name == temp.name &&
       ...
;   
Konstantin
  • 3,254
  • 15
  • 20
  • Thanks! it works. Why use temp though? where did that come from/what does it mean? –  Nov 15 '13 at 20:51
  • it's from your code... `equals` takes object, to be able to compare you need to cast it to `City` – Konstantin Nov 15 '13 at 20:52
-1

eclipse auto-generates for you:

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        long temp;
        temp = Double.doubleToLongBits(latitude);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        temp = Double.doubleToLongBits(longitude);
        result = prime * result + (int) (temp ^ (temp >>> 32));
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((state == null) ? 0 : state.hashCode());
        result = prime * result + zip;
        return result;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        City other = (City) obj;
        if (Double.doubleToLongBits(latitude) != Double
                .doubleToLongBits(other.latitude))
            return false;
        if (Double.doubleToLongBits(longitude) != Double
                .doubleToLongBits(other.longitude))
            return false;
        if (name == null)
        {
            if (other.name != null)
                return false;
        }
        else if (!name.equals(other.name))
            return false;
        if (state == null)
        {
            if (other.state != null)
                return false;
        }
        else if (!state.equals(other.state))
            return false;
        if (zip != other.zip)
            return false;
        return true;
    }
Steve P.
  • 14,489
  • 8
  • 42
  • 72
Greg
  • 652
  • 6
  • 7
  • If you're using Java 7, you could just call [`Objects.hashCode(this)`](http://docs.oracle.com/javase/7/docs/api/java/util/Objects.html#hashCode(java.lang.Object)). If not, use [`Arrays.hashCode(new Object[]{})`](http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#hashCode(java.lang.Object[])) – Luiggi Mendoza Nov 15 '13 at 20:35
  • Is it intentional that a latitude of +0.0 will be considered different from -0.0? – supercat Nov 16 '13 at 00:32