简体   繁体   English

Java-equals()和hashCode()实现

[英]Java - equals() and hashCode() implementation

I have a class PointDensity that is implemented as: 我有一个PointDensity类,它实现为:

import java.sql.Date;

public class PointDensity {

    private int id_place;

    private String algorithm;

    private Date mission_date;

    private int mission_hour;

    private int x;

    private int y;

    public PointDensity(int id_place, String algorithm, Date mission_date, int mission_hour, int x, int y) {

        this.id_place = id_place;
        this.algorithm = algorithm;
        this.mission_date = mission_date;
        this.mission_hour = mission_hour;
        this.x = x;
        this.y = y;
    }


    @Override
    public boolean equals(Object obj){
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        PointDensity other = (PointDensity) obj;
        return id_place == other.id_place
            && algorithm.equals(other.algorithm) 
            && mission_date.equals(other.mission_date)

            && mission_hour == other.mission_hour
            && x == other.x
            && y== other.y;
    }

    @Override
    public int hashCode(){
        final int prime = 31;
        int result = 1;
        result = prime * result + y;
        result = prime * result + ((algorithm == null) ? 0 : algorithm.hashCode());
        result = prime * result + x;
        result = prime * result + ((mission_date == null) ? 0 : mission_date.toString().hashCode());
        result = prime * result + mission_hour;
        result = prime * result + id_place;
        return result;
    }
}

I read some stuff and get values from a database. 我读了一些东西,并从数据库中获取值。 And then I want to use a HashMap() in order to store a PointDensity object as key and the times it has been encountered as a value. 然后,我想使用HashMap()来将PointDensity对象存储为键,并将其遇到的时间存储为一个值。 However, it never finds that the object is the same. 但是,它永远不会发现对象是相同的。

    Map<PointDensity, Integer> pointDensities = new HashMap<>();
    while (resultSet.next()){
        PointDensity pointDensity = 
            new PointDensity(
                resultSet.getInt(1), 
                resultSet.getString(2),
                new Date(timestampValues.getLong(i)), 
                new java.util.Date(timestampValues.getLong(i)).getHours(),
                xValues.getInt(i), 
                yValues.getInt(i)
            );  
        if (pointDensities.containsKey(pointDensity)){
            //IT NEVER ENTERS HERE!!!
            System.out.println("exists");
            int times = pointDensities.get(pointDensity);
            pointDensities.replace(pointDensity, times++);
        }else{
            pointDensities.put(pointDensity, 1);
        }
    }        

Thanks in advance :) 提前致谢 :)

The following code works for me, your hashcode and equals methods seem OK. 以下代码对我hashcode ,您的hashcodeequals方法似乎正常。 The error must be in code you aren't showing us, or in the database data itself. 错误必须出在您未显示给我们的代码中,或者出在数据库数据本身中。

    public static void main( String[] args ) {
       Map<PointDensity, Integer> pointDensities = new HashMap<>();
       PointDensity pointDensity = new PointDensity( 10, "TEST",
               new Date( 3, 1, 2019 ), new java.util.Date().getHours(),
               42, 24 );
       pointDensities.put( pointDensity, 1 );
       System.out.println( "pointDensity equal to self:"
               + pointDensity.equals( pointDensity ) );
       if( pointDensities.containsKey( pointDensity ) ) {

          //IT NEVER ENTERS HERE!!!
          System.out.println( "exists" );
          int times = pointDensities.get( pointDensity );
          pointDensities.replace( pointDensity, times++ );
       } else {
          pointDensities.put( pointDensity, 1 );
       }
    }
}

Output: 输出:

run:
pointDensity equal to self:true
exists
BUILD SUCCESSFUL (total time: 0 seconds)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM