Lombok使用

@EqualsAndHashCode 和@Build用法

  

@EqualsAndHashCode

生成hashCode和equals方法

  可以指定字段:在字段上加@EqualsAndHashCode.Include 表示指定参与的字段,配合类上的@EqualsAndHashCode(onlyExplicitlyIncluded = true)

 

@Build

  会生成builder模式的代码
  需要全参构造,使用该注解会使@Data的无参构造变为全参构造,就没有无参构造了,手动添加无参构造和全参构造的注解,两个都要添加,不然只会有一个
  用法:类名.builder().build();

 

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class Girl {
    @EqualsAndHashCode.Include
    private String name;

    @EqualsAndHashCode.Include
    private String hobby;

    private Integer age;

    private Integer highLength;

    private Integer order;

    private Date birthDate;
}

 

 

 

编译后的代码

自动生成的equals做了非空判断的,不用担心空指异常!!!

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.springboot.demo.webbase.repository;

import java.util.Date;

public class Girl {
    private String name;
    private String hobby;
    private Integer age;
    private Integer highLength;
    private Integer order;
    private Date birthDate;

    public static Girl.GirlBuilder builder() {
        return new Girl.GirlBuilder();
    }

    public String getName() {
        return this.name;
    }

    public String getHobby() {
        return this.hobby;
    }

    public Integer getAge() {
        return this.age;
    }

    public Integer getHighLength() {
        return this.highLength;
    }

    public Integer getOrder() {
        return this.order;
    }

    public Date getBirthDate() {
        return this.birthDate;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public void setHobby(final String hobby) {
        this.hobby = hobby;
    }

    public void setAge(final Integer age) {
        this.age = age;
    }

    public void setHighLength(final Integer highLength) {
        this.highLength = highLength;
    }

    public void setOrder(final Integer order) {
        this.order = order;
    }

    public void setBirthDate(final Date birthDate) {
        this.birthDate = birthDate;
    }

    public String toString() {
        return "Girl(name=" + this.getName() + ", hobby=" + this.getHobby() + ", age=" + this.getAge() + ", highLength=" + this.getHighLength() + ", order=" + this.getOrder() + ", birthDate=" + this.getBirthDate() + ")";
    }

    public Girl() {
    }

    public Girl(final String name, final String hobby, final Integer age, final Integer highLength, final Integer order, final Date birthDate) {
        this.name = name;
        this.hobby = hobby;
        this.age = age;
        this.highLength = highLength;
        this.order = order;
        this.birthDate = birthDate;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof Girl)) {
            return false;
        } else {
            Girl other = (Girl)o;
            if (!other.canEqual(this)) {
                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;
                }

                Object this$hobby = this.getHobby();
                Object other$hobby = other.getHobby();
                if (this$hobby == null) {
                    if (other$hobby != null) {
                        return false;
                    }
                } else if (!this$hobby.equals(other$hobby)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof Girl;
    }

    public int hashCode() {
        int PRIME = true;
        int result = 1;
        Object $name = this.getName();
        int result = result * 59 + ($name == null ? 43 : $name.hashCode());
        Object $hobby = this.getHobby();
        result = result * 59 + ($hobby == null ? 43 : $hobby.hashCode());
        return result;
    }

    public static class GirlBuilder {
        private String name;
        private String hobby;
        private Integer age;
        private Integer highLength;
        private Integer order;
        private Date birthDate;

        GirlBuilder() {
        }

        public Girl.GirlBuilder name(final String name) {
            this.name = name;
            return this;
        }

        public Girl.GirlBuilder hobby(final String hobby) {
            this.hobby = hobby;
            return this;
        }

        public Girl.GirlBuilder age(final Integer age) {
            this.age = age;
            return this;
        }

        public Girl.GirlBuilder highLength(final Integer highLength) {
            this.highLength = highLength;
            return this;
        }

        public Girl.GirlBuilder order(final Integer order) {
            this.order = order;
            return this;
        }

        public Girl.GirlBuilder birthDate(final Date birthDate) {
            this.birthDate = birthDate;
            return this;
        }

        public Girl build() {
            return new Girl(this.name, this.hobby, this.age, this.highLength, this.order, this.birthDate);
        }

        public String toString() {
            return "Girl.GirlBuilder(name=" + this.name + ", hobby=" + this.hobby + ", age=" + this.age + ", highLength=" + this.highLength + ", order=" + this.order + ", birthDate=" + this.birthDate + ")";
        }
    }
}

 

测试

@SpringBootTest
public class ListTest {
    @Test
    public void contextLoads() {
        Girl girl = Girl.builder().name("小小").hobby("爱打乒乓球").age(23).highLength(168).build();

        List<Girl> girls = new ArrayList<>();
        girls.add(girl);
        System.out.println("girls:" + girls.toString());

        List<Girl> pbiEntities2 = new ArrayList<>();
        Girl girls2 = Girl.builder().name("小小").hobby("爱打乒乓球").age(25).highLength(169).build();;
        Girl girls3 = Girl.builder().name("小小").hobby("爱打蓝球").age(27).highLength(170).build();
        pbiEntities2.add(girls2);
        pbiEntities2.add(girls3);

        System.out.println("pbiEntities2:" + pbiEntities2.toString());
     // 认为是同一个  因为只用了name和hobby参与 hash 和 equals
        System.out.println(girls.stream().filter(name -> !pbiEntities2.contains(name)).collect(Collectors.toList()));

    }

 

posted on 2024-04-22 22:45  or追梦者  阅读(8)  评论(0编辑  收藏  举报