Java-equals()和hashCode()实现

时间:2019-01-07 16:54:46

标签: java dictionary data-structures hashmap hashcode

我有一个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;
    }
}

我阅读了一些东西,并从数据库中获取了值。然后,我想使用HashMap()来将PointDensity对象存储为键,并将其遇到的时间存储为一个值。但是,它从未发现对象是相同的。

    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);
        }
    }        

预先感谢:)

1 个答案:

答案 0 :(得分:0)

以下代码对我有用,您的hashcodeequals方法似乎还可以。错误可能出在您未显示给我们的代码中,或者出在数据库数据本身中。

    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 );
       }
    }
}

输出:

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