• lombok @EqualsAndHashCode 注解如何让对象.equals()方法只比较部分属性


    实体类

    import lombok.Data;
    import lombok.EqualsAndHashCode;
    
    
    @Data
    @EqualsAndHashCode
    public class Cs {
        private int id;
        private String name;
    
        public Cs(int id, String name) {
            this.id = id;
            this.name = name;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    运行

        public static void main(String[] args) {
            Cs cs1=new Cs(1,"小小");
            Cs cs2=new Cs(1,"小小");
            System.out.println(cs1.equals(cs2));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    查看生成的class文件

    //
    // Source code recreated from a .class file by IntelliJ IDEA
    // (powered by Fernflower decompiler)
    //
    
    package com.hieasy.rftools.common;
    
    public class Cs {
        private int id;
        private String name;
    
        public Cs(int id, String name) {
            this.id = id;
            this.name = name;
        }
    
        public int getId() {
            return this.id;
        }
    
        public String getName() {
            return this.name;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String toString() {
            return "Cs(id=" + this.getId() + ", name=" + this.getName() + ")";
        }
    
        public boolean equals(Object o) {
            if (o == this) {
                return true;
            } else if (!(o instanceof Cs)) {
                return false;
            } else {
                Cs other = (Cs)o;
                if (!other.canEqual(this)) {
                    return false;
                } else if (this.getId() != other.getId()) {
                    return false;
                } else {
                    Object this$name = this.getName();
                    Object other$name = other.getName();
                    if (this$name == null) {
                        if (other$name != null) {
                            return false;
                        }
                    } else if (!this$name.equals(other$name)) {
                        return false;
                    }
    
                    return true;
                }
            }
        }
    
        protected boolean canEqual(Object other) {
            return other instanceof Cs;
        }
    
        public int hashCode() {
            int PRIME = true;
            int result = 1;
            int result = result * 59 + this.getId();
            Object $name = this.getName();
            result = result * 59 + ($name == null ? 43 : $name.hashCode());
            return result;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76

    如果我们去掉注解是一样的,全部比较的话可以不要@EqualsAndHashCode注解

    //
    // Source code recreated from a .class file by IntelliJ IDEA
    // (powered by Fernflower decompiler)
    //
    
    package com.hieasy.rftools.common;
    
    public class Cs {
        private int id;
        private String name;
    
        public Cs(int id, String name) {
            this.id = id;
            this.name = name;
        }
    
        public int getId() {
            return this.id;
        }
    
        public String getName() {
            return this.name;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public boolean equals(Object o) {
            if (o == this) {
                return true;
            } else if (!(o instanceof Cs)) {
                return false;
            } else {
                Cs other = (Cs)o;
                if (!other.canEqual(this)) {
                    return false;
                } else if (this.getId() != other.getId()) {
                    return false;
                } else {
                    Object this$name = this.getName();
                    Object other$name = other.getName();
                    if (this$name == null) {
                        if (other$name != null) {
                            return false;
                        }
                    } else if (!this$name.equals(other$name)) {
                        return false;
                    }
    
                    return true;
                }
            }
        }
    
        protected boolean canEqual(Object other) {
            return other instanceof Cs;
        }
    
        public int hashCode() {
            int PRIME = true;
            int result = 1;
            int result = result * 59 + this.getId();
            Object $name = this.getName();
            result = result * 59 + ($name == null ? 43 : $name.hashCode());
            return result;
        }
    
        public String toString() {
            return "Cs(id=" + this.getId() + ", name=" + this.getName() + ")";
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77

    但是我们如果只想比较name属性的话,类上需要加入@EqualsAndHashCode(onlyExplicitlyIncluded = true),想只比较哪些属性就在属性上加@EqualsAndHashCode.Include

    import lombok.Data;
    import lombok.EqualsAndHashCode;
    
    /**
     * @author Zc
     * @date 2022年06月24日 16:04
     */
    @Data
    @EqualsAndHashCode(onlyExplicitlyIncluded = true)
    public class Cs {
        private int id;
        @EqualsAndHashCode.Include
        private String name;
    
        public Cs(int id, String name) {
            this.id = id;
            this.name = name;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    结果
    在这里插入图片描述

  • 相关阅读:
    Java8函数式编程-lambda表达式与stream流
    Linux 忘记密码怎么办,CentOS和Ubuntu重置密码方法
    一起来理解计算机系统(2)_信息表示与处理
    Revit中如何彻底删除房间标记及“项目族管理”
    缓存穿透、缓存雪崩、缓存击穿(一张表清晰明了)
    Qt 简介
    振弦采集仪安全监测路基边坡的解决方案
    Pandas数据分析及可视化应用实践
    报告(2022-7-26)
    18-Java迭代器模式 ( Iterator Pattern )
  • 原文地址:https://blog.csdn.net/qq_38030465/article/details/125447630