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);
}
}