判断类的两个对象是否相同要重新实现equals方法

  1 import java.util.Objects;
  2 
  3 public class ObjectProperty {
  4     public String devId;
  5     public long timestamp;
  6     public double latitude;
  7     public double longitude;
  8     public int satellite;
  9     public double hdop;
 10     public double altitude;
 11     public double speed;
 12     public double direction;
 13     public ObjectProperty(String devId2, long timestamp2, double latitude2, double longitude2, int satellite2,
 14             double hdop2, double altitude2, double speed2, double direction2) {
 15         this.devId=devId2;this.timestamp=timestamp2;this.latitude=latitude2;this.longitude=longitude2;this.satellite=satellite2;this.hdop=hdop2;this.altitude=altitude2;this.speed=speed2;this.direction=direction2;
 16     }
 17     public String getDevId() {
 18         return devId;
 19     }
 20     public void setDevId(String devId) {
 21         this.devId = devId;
 22     }
 23     public long getTimestamp() {
 24         return timestamp;
 25     }
 26     public void setTimestamp(long timestamp) {
 27         this.timestamp = timestamp;
 28     }
 29     public double getLatitude() {
 30         return latitude;
 31     }
 32     public void setLatitude(double latitude) {
 33         this.latitude = latitude;
 34     }
 35     public double getLongitude() {
 36         return longitude;
 37     }
 38     public void setLongitude(double longitude) {
 39         this.longitude = longitude;
 40     }
 41     public int getSatellite() {
 42         return satellite;
 43     }
 44     public void setSatellite(int satellite) {
 45         this.satellite = satellite;
 46     }
 47     public double getHdop() {
 48         return hdop;
 49     }
 50     public void setHdop(double hdop) {
 51         this.hdop = hdop;
 52     }
 53     public double getAltitude() {
 54         return altitude;
 55     }
 56     public void setAltitude(double altitude) {
 57         this.altitude = altitude;
 58     }
 59     public double getSpeed() {
 60         return speed;
 61     }
 62     public void setSpeed(double speed) {
 63         this.speed = speed;
 64     }
 65     public double getDirection() {
 66         return direction;
 67     }
 68     public void setDirection(double direction) {
 69         this.direction = direction;
 70     }
 71     @Override
 72     public String toString() {
 73         return "ObjectProperty [devId=" + devId + ", timestamp=" + timestamp + ", latitude=" + latitude + ", longitude="
 74                 + longitude + ", satellite=" + satellite + ", hdop=" + hdop + ", altitude=" + altitude + ", speed="
 75                 + speed + ", direction=" + direction + "]";
 76     }
 77     /**
 78      * 判断两个对象时候相等必须重写这个方法
 79      */
 80     @Override
 81     public boolean equals(Object obj) {
 82         if(this==obj)
 83             return true;
 84         if(obj==null||getClass()!=obj.getClass())
 85             return false;
 86         ObjectProperty objectProperty=(ObjectProperty) obj;
 87         return Objects.equals(devId, objectProperty.devId)&&
 88                 Objects.equals(timestamp, objectProperty.timestamp)&&
 89                 Objects.equals(latitude, objectProperty.latitude)&&
 90                 Objects.equals(longitude, objectProperty.longitude)&&
 91                 Objects.equals(satellite, objectProperty.satellite)&&
 92                 Objects.equals(hdop, objectProperty.hdop)&&
 93                 Objects.equals(altitude, objectProperty.altitude)&&
 94                 Objects.equals(speed, objectProperty.speed)&&
 95                 Objects.equals(direction, objectProperty.direction);
 96         //return super.equals(obj);
 97     }
 98     @Override
 99     public int hashCode() {
100         return this.devId.hashCode();
101         //return super.hashCode();
102     }
103     
104 }

首先如果是同一个对象那么就返回真

如果不是同一个类绝对为假

如果两个对象的所有关注的属性全部相同那么可以两个对象相同

另外hashcode的简单覆盖是直接返回唯一id的hashcode而不是类的hashcode那么如果唯一的id是相同的话,可以认为两个对象的Hashcode相同时同一个对象

posted @ 2018-11-06 10:54  kongchung  阅读(285)  评论(0编辑  收藏  举报