美文网首页
计算机应用实践之--重写Java对象HashCode

计算机应用实践之--重写Java对象HashCode

作者: 献计献策 | 来源:发表于2017-12-15 11:49 被阅读0次

我们系统有一个场景,根据我们业务自己定义的商家拆分规则,将用户提交的发票申请拆分成多个发票(运费、订单实收),重写hashCode的原因还是因为我们用到了HashMap这个数据结构。
将拆分规则作为Key,用户的订单列表作为Value用来计算总金额。
下面是代码重写equals()方法就会去重写hashCode计算。
参考 wiki hashcode链接

@Getter
@Setter
public class ApplicationSplitRule  implements Serializable{
    private Long id ;
    private String splitRuleName;//拆分规则名称
    private Integer supplyTypeId;//支持类型
    private String supplyTypeName;//支持类型名称
    private String vendorGroupIdList;//商家分组ID列表
    private String vendorGroupNameList;//商家分组名称列表
    private Integer invoiceAmountSource;//金额来源  运费,订单实收

    @Override
    public int hashCode() {
        int hash = 1;//选点质数 
        hash = hash * 13 + getId().hashCode();
        hash = hash * 17 + getSupplyTypeId().hashCode();
        hash = hash * 23 + getVendorGroupIdList().hashCode();
        hash = hash * 31 + getInvoiceAmountSource().hashCode();
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if(this == obj){
            return  true;
        }
        if(obj instanceof ApplicationSplitRule){
            ApplicationSplitRule rule = (ApplicationSplitRule) obj;
            if (this.getId().equals(rule.getId())
                    &&this.getSupplyTypeId().equals(rule.getSupplyTypeId())
                    &&this.getVendorGroupIdList().equals(rule.getVendorGroupIdList())
                    && this.getInvoiceAmountSource().equals(rule.getInvoiceAmountSource())) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }
}

相关文章

  • 计算机应用实践之--重写Java对象HashCode

    我们系统有一个场景,根据我们业务自己定义的商家拆分规则,将用户提交的发票申请拆分成多个发票(运费、订单实收),重写...

  • 计算机应用实践之---重写对象HashCode算法

    @Getter @Setter public class ApplicationSplitRule impleme...

  • hashcode

    JAVA中重写equals()方法的同时要重写hashcode()方法 object对象中的 public boo...

  • java 对象根据属性去重

    java 对象去重 实体类需要重写hashCode() 和 eqauls() 如此即可

  • Object类

    找对象的方式: Object 类: java规范:一般我们再重写equals方法的时候,我们都会重写hashCode方法

  • Java中hashCode的实现

    Java中hashCode的实现 从我们刚学Java就知道,要重写equal就要一起重写hashCode.但是你有...

  • Java 比较相等

    Java Equals() 特性 Equals() 和 == 的区别 重写Equals方法 重写HashCode方法

  • hashCode记录

    Java基本都会重写Object中的hashCode方法,归类记录hashCode的重写 基本数据类型 基本数据类...

  • 53 HashMap8 源码解读

    1,为什么重写equals还要重写hashcode方法hashcode方法:底层采用c语言编写的,根据对象内存地址...

  • Java的equals和hashCode方法

    对于equals和hashCode 方法,Java建议在重写时应当满足规定: 如果两对象equals为true,h...

网友评论

      本文标题:计算机应用实践之--重写Java对象HashCode

      本文链接:https://www.haomeiwen.com/subject/jnwrwxtx.html