自定义HashMap的键

  用自定义的类型作为HashMap的key,必须同时重载hashCode()和equals(),才可以实现在HashMap中的查找自定义键。

  例如自定义Point类:

public class Point {
        private int x;
        private int y;

        public Point(int x,int y) {
            this.x = x;
            this.y = y;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getX() {
            return x;
        }

        public void setY(int y) {
            this.y = y;
        }

        public int getY() {
            return y;
        }

        @Override
        public String toString() {
            return "["+x+","+y+"]";
        }
}
View Code

  示例:

  clusters数据示例:<String, Set<Point>> <用户,所有簇中心坐标列表>

  Point作为Map的键:

Map<Point, Set<String>> usersOfCertainPoint = new LinkedHashMap<Point, Set<String>>();//<网格坐标, 聚类中心点为该坐标的所有用户列表>
Iterator<Map.Entry<String, Set<Point>>> iterator = clusters.entrySet().iterator();
while(iterator.hasNext()){
    Map.Entry<String, Set<Point>> entry = iterator.next();
    for(Point p : entry.getValue()){
        if(!usersOfCertainPoint.containsKey(p)){//containsKey()出错
            Set<String> userSet = new HashSet<String>();
            userSet.add(entry.getKey());
            usersOfCertainPoint.put(p, userSet);
        }else{
            usersOfCertainPoint.get(p).add(entry.getKey());
        }
    }
}
View Code

   错误输出示例:

  问题出在Point自动地继承自基类Object,所以这里使用Object的hashCode()方法生成散列码,而它默认是使用对象的地址计算散列码。因此即使是两个值相同的Point对象如[4515,-8198],因为其地址不同,所以生成的散列码不同。

  若只重载hashCode(),还是无法正常运行,必须同时重载equals()方法。在HashMap中,查找key的比较顺序为:

  1. 计算对象的hashCode,看在表中是否存在;
  2. 检查对应hashCode位置中的对象和当前对象是否相等。

  重载hashCode()和equals():

           @Override
        public boolean equals(Object o){
            if(this == o)//是否是本类的一个引用
                return true;
            if(!(o instanceof Point))
                return false;
            Point p = (Point)o;
            return (p.x == this.x) && (p.y == this.y);
        }
        @Override
        public int hashCode(){
            int result = 17;
            result = 31 * result + x;
            result = 31 * result + y;
            return result;
        }            
View Code

  正确输出示例:

原文地址:https://www.cnblogs.com/-1307/p/6430918.html