• 【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
  • 相关阅读:
    把字符串输入到表格里
    x 的 x 次方等于10,求 x
    java 中 二进制串与浮点数的相互转化
    堆栈 Objective-C NSString copy strong
    c一些学习过程中突然错过的细节
    视图控制器
    名词从句
    FastDFS
    Python
    http 提交表单数据
  • 原文地址:https://www.cnblogs.com/shangxiaofei/p/9245157.html
Copyright © 2020-2023  润新知