一种自定义的计算java对象hash值的方式

HashCode

java Object对象自带hashCode函数,默认所有对象的hashCode都不相等。但是java自带的原始类型更改了hashCode的计算方式,所以所有对象的hashCode都可以是基于基本类型的hashCode组合

Lombok

lombok的EqualsAndHash拥有我们要求的绝大多数特性:
1,自动扫描字段
2,可配置的过滤字段
但是有个业务要求是,不关心集合类型的元素顺序,这用lombok办不到

自定义hash实现

1,自动扫描字段
2,可配置的过滤字段
3,不关心集合类型中元素的顺序
4,嵌套计算field

在这里插入图片描述
CalcIdUtil: 主要实现
IgnoreField:过滤注解
Inner, Outer, Messages:测试类,main函数在Outer

实现

package com.example.demo.idcode;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.Collection;

@Slf4j
public class CalcIdUtil {
  @SneakyThrows
  public static int calc(Object target) {
    if (isPrimitive(target)) {
      return target.hashCode();
    }

    int id = 999;
    for (var field : target.getClass().getDeclaredFields()) {
      if (field.getAnnotation(IgnoreField.class) != null) {
        log.info("ignoring {}.", field.getName());
        continue;
      }

      field.setAccessible(true);
      Object fieldValue = field.get(target);

      if (isPrimitive(fieldValue)) {
        log.info("adding {} of primitive type {}", field.getName(), fieldValue.getClass());
        id ^= fieldValue.hashCode();
        continue;
      }

      if (fieldValue instanceof Collection) {
        for (Object o : (Collection) fieldValue) {
          log.info("adding {} of type {} in collection {}", o, o.getClass(), field.getName());
          id ^= calc(o);
        }

        continue;
      }

      log.info("adding {} of class {}", field.getName(), fieldValue.getClass());
      id ^= calc(fieldValue);
    }

    return id;
  }

  private static boolean isPrimitive(Object o) {
    var c = o.getClass();
    return c == Byte.class
        || c == Short.class
        || c == Integer.class
        || c == Long.class
        || c == Float.class
        || c == Double.class
        || c == Boolean.class
        || c == String.class;
  }
}

注解

package com.example.demo.idcode;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface IgnoreField {}

测试代码

package com.example.demo.idcode;

import java.util.List;
import java.util.Set;

public class Outer {
  private Inner inner;
  private List<Inner> innerList;
  private Set<Inner> innerSet;
  private Messages messages;

  public Outer(Inner inner, List<Inner> innerList, Set<Inner> innerSet, Messages messages) {
    this.inner = inner;
    this.innerList = innerList;
    this.innerSet = innerSet;
    this.messages = messages;
  }

  public static void main(String[] args) {
    Outer outer1 =
        new Outer(
            new Inner(1, "mary"),
            List.of(new Inner(11, "mary"), new Inner(12, "freddie")),
            Set.of(new Inner(21, "mary"), new Inner(22, "parker")),
            new Messages(List.of("hello", "world")));
    Outer outer2 =
        new Outer(
            new Inner(10, "mary"),
            List.of(new Inner(120, "freddie"), new Inner(11, "mary")),
            Set.of(new Inner(220, "parker"), new Inner(21, "mary")),
            new Messages(List.of("world", "hello")));

    System.out.println(CalcIdUtil.calc(outer1) == CalcIdUtil.calc(outer2));
  }
}

16:23:59.413 [main] INFO com.example.demo.idcode.CalcIdUtil - adding inner of class class com.example.demo.idcode.Inner
16:23:59.424 [main] INFO com.example.demo.idcode.CalcIdUtil - ignoring age.
16:23:59.424 [main] INFO com.example.demo.idcode.CalcIdUtil - adding name of primitive type class java.lang.String
16:23:59.424 [main] INFO com.example.demo.idcode.CalcIdUtil - adding com.example.demo.idcode.Inner@77f99a05 of type class com.example.demo.idcode.Inner in collection innerList
16:23:59.424 [main] INFO com.example.demo.idcode.CalcIdUtil - ignoring age.
16:23:59.424 [main] INFO com.example.demo.idcode.CalcIdUtil - adding name of primitive type class java.lang.String
16:23:59.424 [main] INFO com.example.demo.idcode.CalcIdUtil - adding com.example.demo.idcode.Inner@63440df3 of type class com.example.demo.idcode.Inner in collection innerList
16:23:59.424 [main] INFO com.example.demo.idcode.CalcIdUtil - ignoring age.
16:23:59.424 [main] INFO com.example.demo.idcode.CalcIdUtil - adding name of primitive type class java.lang.String
16:23:59.424 [main] INFO com.example.demo.idcode.CalcIdUtil - adding com.example.demo.idcode.Inner@3aeaafa6 of type class com.example.demo.idcode.Inner in collection innerSet
16:23:59.424 [main] INFO com.example.demo.idcode.CalcIdUtil - ignoring age.
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - adding name of primitive type class java.lang.String
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - adding com.example.demo.idcode.Inner@76a3e297 of type class com.example.demo.idcode.Inner in collection innerSet
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - ignoring age.
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - adding name of primitive type class java.lang.String
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - adding hello of type class java.lang.String in collection messages
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - adding world of type class java.lang.String in collection messages
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - adding inner of class class com.example.demo.idcode.Inner
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - ignoring age.
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - adding name of primitive type class java.lang.String
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - adding com.example.demo.idcode.Inner@ed9d034 of type class com.example.demo.idcode.Inner in collection innerList
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - ignoring age.
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - adding name of primitive type class java.lang.String
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - adding com.example.demo.idcode.Inner@6121c9d6 of type class com.example.demo.idcode.Inner in collection innerList
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - ignoring age.
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - adding name of primitive type class java.lang.String
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - adding com.example.demo.idcode.Inner@87f383f of type class com.example.demo.idcode.Inner in collection innerSet
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - ignoring age.
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - adding name of primitive type class java.lang.String
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - adding com.example.demo.idcode.Inner@4eb7f003 of type class com.example.demo.idcode.Inner in collection innerSet
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - ignoring age.
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - adding name of primitive type class java.lang.String
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - adding world of type class java.lang.String in collection messages
16:23:59.425 [main] INFO com.example.demo.idcode.CalcIdUtil - adding hello of type class java.lang.String in collection messages
true

可以看到outer1 & outer2被ignore的字段以及集合的顺序不影响最终结果.

package com.example.demo.idcode;

public class Inner {
  @IgnoreField private int age;

  private String name;

  public Inner(int age, String name) {
    this.age = age;
    this.name = name;
  }
}
package com.example.demo.idcode;

import java.util.Collection;
import java.util.LinkedList;

public class Messages extends LinkedList<String> {
  public Messages(Collection<? extends String> c) {
    super(c);
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值