zoukankan      html  css  js  c++  java
  • 【java编程】重写HashCode和equals方法

    【一】重写equals方案的规则

    equals方法本来的原则 

    1、类的每个实例本质上都是唯一的。

    2、不关心类是否提供了“逻辑相等”的测试功能

    3、超类已经覆盖了equals,从超类继承过来的行为对于子类也是合适的。

    4、类是自有的活是包级私有的,可以确定equals方法永远不会被调用。这个时候就要重写,防止被外界调用。

    equals方法实现了等价关系。

    1、自反性:对于任何非null的引用值x.则必须满足x.equals(x)必须返回true

    2、对称性:对于任何非null的引用x,y.   x.equals(y)返回true,则y.equals(x)必须返回true

    3、传递性:对于任何非null的引用x,y,z。则x.equals(y)返回true,y.equals(z)返回true.则x.equals(z)必须返回true

    4、一致性:对于任何非null的引用x,y。只要对象里的信息没有被修改。x.equals(y)多次调用,要么一直返回true,要么一直返回false

    5、对于任何非null的引用值x.x.equals(null)必须返回false
    【二】重写hashCode方法的规则

    覆盖了equals时,总要覆盖hashCode方法。

    一个重写hashCode和equals的实例

    import org.apache.commons.lang3.StringUtils;
    
    /**
     * 
     */
    public final class Category {
        private volatile int hashCode = 0;
        private final boolean nullOrEmptyIsCached;
        private final int initialCapacity;
        private final long maxSize;
        private final long timeOut;
        private final String cacheKey;
    
        public Category(int initialCapacity, long maxSize, long timeOut, String cacheKey, boolean nullOrEmptyIsCached) {
            this.initialCapacity = initialCapacity;
            this.maxSize = maxSize;
            this.timeOut = timeOut;
            this.cacheKey = cacheKey;
            this.nullOrEmptyIsCached = nullOrEmptyIsCached;
        }
    
        @Override
        public int hashCode() {
            int internalHashCode = hashCode;
            if (internalHashCode == 0) {
                int nullOrEmptyIsCachedInt = nullOrEmptyIsCached ? 1 : 0;
                internalHashCode = 31 * internalHashCode + nullOrEmptyIsCachedInt;
                internalHashCode = 31 * internalHashCode + initialCapacity;
                int mS = (int) (maxSize ^ (maxSize >>> 32));
                internalHashCode = 31 * internalHashCode + mS;
                int tO = (int) (timeOut ^ (timeOut >>> 32));
                internalHashCode = 31 * internalHashCode + tO;
                if (StringUtils.isNotEmpty(cacheKey)) {
                    internalHashCode = 31 * internalHashCode + cacheKey.hashCode();
                }
                hashCode = internalHashCode;
            }
            return internalHashCode;
        }
    
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (!(obj instanceof Category)) {
                return false;
            }
            Category otherCategory = (Category) obj;
            return this.nullOrEmptyIsCached == otherCategory.nullOrEmptyIsCached && this.initialCapacity == otherCategory.initialCapacity && this.maxSize == otherCategory.maxSize && this.timeOut == otherCategory.timeOut && (this.cacheKey == null ? this.cacheKey == otherCategory.cacheKey : this.cacheKey.equals(otherCategory.cacheKey));
        }
    //省略sett,gett
    }
    View Code
  • 相关阅读:
    今天想写东东
    [BTS]Event ID: 5803 错误处理办法
    微软技术能力测试工具V0.1试用
    PMI,PMBOK,PMP,Input,Output,Update
    [BTS]5719, 5753错误处理?
    向您推荐Groove BizTalk Workspace!
    [BTS]Adapter Pack调用SAP RFC接口时间格式出错解决办法
    [BTS]BizTalk学习之Functoid篇(ID CrossReferences)
    健身减肥计划
    [BTS] Event ID 5740
  • 原文地址:https://www.cnblogs.com/shangxiaofei/p/9245157.html
Copyright © 2011-2022 走看看