LomBok 的相关目录已经整理出来,希望大家可以根据需求自助学习,好工具要大家分享:
实现equals, hashCode方法是在编程生活中再常见不过的一个东西了,那么自然@EqualsAndHashCode 这个annotation就成为了一个非常方便的工具。默认情况下,被这个annotation标注的class会用到除了 static,transient修饰的所有属性作为判断标准,当然和之前的annotation一样,可是使用exclude选项除掉不想要的属性。也可以通过callSuper包含父类的equals 和 hashCode。 当然如果你的class 没有继承任何其他的class,你却写了callSuper,那么会收获一个编译报错。
你只要这样用:
import lombok.EqualsAndHashCode; @EqualsAndHashCode(exclude={"id", "shape"}) public class EqualsAndHashCodeExample { private transient int transientVar = 10; private String name; private double score; private Shape shape = new Square(5, 10); private String[] tags; private int id; public String getName() { return this.name; } @EqualsAndHashCode(callSuper=true) public static class Square extends Shape { private final int width, height; public Square(int width, int height) { this.width = width; this.height = height; } } }
就可以得到这样的效果:
import java.util.Arrays; public class EqualsAndHashCodeExample { private transient int transientVar = 10; private String name; private double score; private Shape shape = new Square(5, 10); private String[] tags; private int id; public String getName() { return this.name; } @Override public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof EqualsAndHashCodeExample)) return false; EqualsAndHashCodeExample other = (EqualsAndHashCodeExample) o; if (!other.canEqual((Object)this)) return false; if (this.getName() == null ? other.getName() != null : !this.getName().equals(other.getName())) return false; if (Double.compare(this.score, other.score) != 0) return false; if (!Arrays.deepEquals(this.tags, other.tags)) return false; return true; } @Override public int hashCode() { final int PRIME = 59; int result = 1; final long temp1 = Double.doubleToLongBits(this.score); result = (result*PRIME) + (this.name == null ? 0 : this.name.hashCode()); result = (result*PRIME) + (int)(temp1 ^ (temp1 >>> 32)); result = (result*PRIME) + Arrays.deepHashCode(this.tags); return result; } protected boolean canEqual(Object other) { return other instanceof EqualsAndHashCodeExample; } public static class Square extends Shape { private final int width, height; public Square(int width, int height) { this.width = width; this.height = height; } @Override public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Square)) return false; Square other = (Square) o; if (!other.canEqual((Object)this)) return false; if (!super.equals(o)) return false; if (this.width != other.width) return false; if (this.height != other.height) return false; return true; } @Override public int hashCode() { final int PRIME = 59; int result = 1; result = (result*PRIME) + super.hashCode(); result = (result*PRIME) + this.width; result = (result*PRIME) + this.height; return result; } protected boolean canEqual(Object other) { return other instanceof Square; } } }
这就是Lombok的优势所在。几个需要注意的点是,对于Array类型的变量,如果需要进行equals和hashCode的时候需要调用工具类的Arrays中的deepHashCode, deepEquals完成比较。
相关推荐
无参构造器、部分参数构造器、全参构造器,当我们需要重载多个构造器的时候,Lombok就无能为力了。 @Data @ToString, @EqualsAndHashCode, 所有属性的@Getter, 所有non-final属性的@Setter和@...
Lombok项目是一个java库,它可以自动插入到编辑器和构建工具中,增强java的性能。不需要再写getter、setter或equals方法,只要有一个注解,你的类就有一个功能齐全的构建器、自动记录变量等等。 Lombok常用注解编辑...
Lombok 是一个 Java 库,它通过使用注解来消除样板代码(boilerplate code),从而简化 Java 类的开发过程。使用 Lombok 可以让开发者在编写 Java 类时减少冗长的代码,提高代码的可读性和可维护性。 Lombok 提供了...
idea插件,使用了lombok的注解(@Setter,@Getter,@ToString,@@RequiredArgsConstructor,@EqualsAndHashCode或@Data)之后,就不需要编写或生成get/set等方法,很大程度上减少了代码量
功能/支持 @EqualsAndHashCode @ AllArgsConstructor,@ RequiredArgsConstructor和@NoArgsConstructor @日志@ slf4j @数据@Builder @单数@代表@价值@Accessors @枯萎@SneakyThrows @val @UtilityClass 我有问题
@EqualsAndHashCode ( callSuper = false ) public Member extends Row< Member> { private long id; private String name; } 例子 创建新的数据库对象。 Connection connection = DriverManager . getConnection...
lombok,个人整理的xmind格式笔记。包含:原理,安装使用,@Getter/@Setter,@ToString,@EqualsAndHashCode等常用注释
Discussing code is now as easy as highlighting a block and typing a comment right from your IDE. Take the pain out of code reviews...@EqualsAndHashCode @AllArgsConstructor, @RequiredArgsConstructor and @
mybatis 逆向工程插件,用来生成lombok Get Set相关方法:<property name="lombok" value="Getter,Setter,Data,ToString,Accessors,EqualsAndHashCode"/>
3. @EqualsAndHashCode(of = {"sentence", "val"}) 4. @ToString(exclude = "val") 5. @Builder 6. @RequiredArgsConstructor, generates a constructor for all final fields, with parameter order same as field
Lombok2、引入依赖3、在 Eclipse 中安装 Lombok 插件1)准备 Jar 包2)安装 Lombok 插件3)查看是否安装成功4)重启 IDE 工具二、常用注解1、@Getter/@Setter2、@NonNull3、@ToString4、@EqualsAndHashCode5、@...
自定义MyBatisGenerator中model生成,增加Lombok中的@Data及@EqualsAndHashCode,将类及变量注释修改为数据库中的注释信息
自定义MyBatisGenerator中model生成,增加Lombok中的@Data及@EqualsAndHashCode,将类及变量注释修改为数据库中的注释信息