zoukankan      html  css  js  c++  java
  • android开发重写equals方法和hashCode方法的通用写法记录

    实际开发我们有时需要判断比较两个对象是否相同,通常做法是重写对象的equals方法。
    但重写equals方法时,一般我们也会重写hashCode方法。其实如果该对象不会当作Map里的key,不重写hashCode方法也是没啥影响的。
    想重写hashCode方法不知道该怎么写?下面是重写equals方法时,也重写hashCode方法的通用写法:

    final
    class ResourceCacheKey implements Key { private final Key sourceKey; private final Key signature; private final int width; private final int height;private final Options options; @Override public boolean equals(Object o) { if (o instanceof ResourceCacheKey) { ResourceCacheKey other = (ResourceCacheKey) o; return height == other.height && width == other.width&& sourceKey.equals(other.sourceKey) && signature.equals(other.signature) && options.equals(other.options); } return false; } @Override public int hashCode() { int result = sourceKey.hashCode(); result = 31 * result + signature.hashCode(); result = 31 * result + width; result = 31 * result + height; result = 31 * result + options.hashCode(); return result; } }
  • 相关阅读:
    【洛谷P3628】特别行动队
    【洛谷P3233】世界树
    【BZOJ1597】土地购买
    【洛谷P4068】数字配对
    【洛谷P3899】谈笑风生
    【BZOJ2726】任务安排
    【洛谷P6186】[NOI Online 提高组] 冒泡排序
    【洛谷P3369】【模板】普通平衡树
    【UOJ#8】Quine
    标准 插入flash
  • 原文地址:https://www.cnblogs.com/yongfengnice/p/12009547.html
Copyright © 2011-2022 走看看