`
朱秋旭
  • 浏览: 228043 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Lombok 之 EqualsAndHashCode

阅读更多

LomBok 的相关目录已经整理出来,希望大家可以根据需求自助学习,好工具要大家分享:

@Cleanup     

@Getter, @Setter

@ToString

@EqualsAndHashCode

@Constructor

@Data & @Value

@SneakyThrows

@Synchronized

@Getter(lazy=true)

@Log

 

实现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完成比较。

1
0
分享到:
评论
1 楼 cuisuqiang 2014-10-03  
不错,自定义对象对比方法的实现

相关推荐

    lombok-1.16.16源码和文档

      无参构造器、部分参数构造器、全参构造器,当我们需要重载多个构造器的时候,Lombok就无能为力了。 @Data   @ToString, @EqualsAndHashCode, 所有属性的@Getter, 所有non-final属性的@Setter和@...

    lombok的jar包

    Lombok项目是一个java库,它可以自动插入到编辑器和构建工具中,增强java的性能。不需要再写getter、setter或equals方法,只要有一个注解,你的类就有一个功能齐全的构建器、自动记录变量等等。 Lombok常用注解编辑...

    Lombok(Java库)

    Lombok 是一个 Java 库,它通过使用注解来消除样板代码(boilerplate code),从而简化 Java 类的开发过程。使用 Lombok 可以让开发者在编写 Java 类时减少冗长的代码,提高代码的可读性和可维护性。 Lombok 提供了...

    lombok-idea插件

    idea插件,使用了lombok的注解(@Setter,@Getter,@ToString,@@RequiredArgsConstructor,@EqualsAndHashCode或@Data)之后,就不需要编写或生成get/set等方法,很大程度上减少了代码量

    vscode-lombok:Lombok注释Visual Studio Code的官方扩展

    功能/支持 @EqualsAndHashCode @ AllArgsConstructor,@ RequiredArgsConstructor和@NoArgsConstructor @日志@ slf4j @数据@Builder @单数@代表@价值@Accessors @枯萎@SneakyThrows @val @UtilityClass 我有问题

    tinyorm:适用于Java的微型OR映射器

    @EqualsAndHashCode ( callSuper = false ) public Member extends Row< Member> { private long id; private String name; } 例子 创建新的数据库对象。 Connection connection = DriverManager . getConnection...

    lombok笔记.xmind

    lombok,个人整理的xmind格式笔记。包含:原理,安装使用,@Getter/@Setter,@ToString,@EqualsAndHashCode等常用注释

    lombok-plugin-0.31-2020.1.zip

    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 @

    mapper-generator-1.1.1

    mybatis 逆向工程插件,用来生成lombok Get Set相关方法:<property name="lombok" value="Getter,Setter,Data,ToString,Accessors,EqualsAndHashCode"/>

    lombok-examples:Using使用Lombok的实践示例:https:projectlombok.org

    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

    Lombok初体验

    Lombok2、引入依赖3、在 Eclipse 中安装 Lombok 插件1)准备 Jar 包2)安装 Lombok 插件3)查看是否安装成功4)重启 IDE 工具二、常用注解1、@Getter/@Setter2、@NonNull3、@ToString4、@EqualsAndHashCode5、@...

    mybatis-generator.zip

    自定义MyBatisGenerator中model生成,增加Lombok中的@Data及@EqualsAndHashCode,将类及变量注释修改为数据库中的注释信息

    mybatis-generator-core-1.4.1-SNAPSHOT.jar

    自定义MyBatisGenerator中model生成,增加Lombok中的@Data及@EqualsAndHashCode,将类及变量注释修改为数据库中的注释信息

Global site tag (gtag.js) - Google Analytics