zoukankan      html  css  js  c++  java
  • hashcode-equals方法

    package com.charles.collection;
    
    import java.util.HashSet;
    import java.util.Set;
    
    public class Point {
        /**
         * @author Charles
         * @desc introduce hashcode and equals methods
         */
    
        private Integer x;
        private Integer y;
    
        public Point() {
        }
    
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    
        public static void main(String[] args) {
            /*如果不重写equals方法,one与two对象就不相等,这是因为Point类
             * 默认继承父类Object的equals方法,而Object类的equals方法
             * 比较的是引用相等
             */
            Point one = new Point(3,3);
            Point two = new Point(3,3);
            if(one.equals(two)){
                System.out.println("one equals two..");
            }else{
                System.out.println("one NOT equals two..");//注释掉当前类的equals方法,可输出该结果
            }
            
            /*
             * 这些又与hashcode方法有什么关系呢?实际上如果Point类不参与
             * 与hash算法相关的存储运算,重写hashcode方法是没有必要的。
             */
            
            Set<Point> sets = new HashSet<Point>();
            sets.add(one);
            sets.add(two);
            //注释掉hashcode与equals方法,输出结果:2;否则为1, 原因是对象one与two的hash值相等,且相互equals,先放进set集合中的对象被后者覆盖了
            System.out.println(sets.size()); 
            /*
             * 猜猜留下的那个唯一对象是谁呢?
             * 测试方法可用==判断
             */
            if(one == sets.iterator().next()){
                System.out.println("Yeah, one left~~");
            }else if(one == sets.iterator().next()){
                System.out.println("Oh, two left~~");
            }
            
            //实际结果是one,这是因为当set集合中已经存在,hashcode与equals相等的元素时,便不再将当前元素放入集合了,这也可以通过add方法的返回值检测
        }
    
        public Integer getX() {
            return x;
        }
    
        public void setX(Integer x) {
            this.x = x;
        }
    
        public Integer getY() {
            return y;
        }
    
        public void setY(Integer y) {
            this.y = y;
        }
    
        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((x == null) ? 0 : x.hashCode());
            result = prime * result + ((y == null) ? 0 : y.hashCode());
            return result;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Point other = (Point) obj;
            if (x == null) {
                if (other.x != null)
                    return false;
            } else if (!x.equals(other.x))
                return false;
            if (y == null) {
                if (other.y != null)
                    return false;
            } else if (!y.equals(other.y))
                return false;
            return true;
        }
    
        @Override
        public String toString() {
            return "Point [x=" + x + ", y=" + y + "]";
        }
    
    }
  • 相关阅读:
    linux的常用命令--pwd命令【持续补充ing】
    linux的常用命令--cp命令【持续补充ing】
    linux的常用命令--mkdir【持续补充ing】
    linux的常用命令--su篇【持续补充ing】
    linux的常用命令--ls篇【持续补充ing】
    linux的常用命令--cd篇【持续补充ing】
    【SQL Server 2008数据库基础篇】常用关键字、数据类型和常用语法
    SpingBoot之多Profile文件
    SpringBoot之YAML
    SpringBoot之HelloWorld仔细分析
  • 原文地址:https://www.cnblogs.com/itachy/p/7203208.html
Copyright © 2011-2022 走看看