lombok.EqualsAndHashCode
时间: 2023-10-14 15:02:30 浏览: 104
lombok.EqualsAndHashCode is a Lombok annotation that generates `equals()` and `hashCode()` methods for a class. This annotation helps to reduce boilerplate code and ensures that the `equals()` and `hashCode()` methods are implemented consistently.
To use the `@EqualsAndHashCode` annotation, you simply need to annotate your class with it. Lombok will then generate the `equals()` and `hashCode()` methods based on the fields of the class.
Here's an example:
```java
import lombok.EqualsAndHashCode;
@EqualsAndHashCode
public class MyClass {
private int id;
private String name;
// other fields and methods
}
```
In this example, Lombok will generate the `equals()` and `hashCode()` methods for the `MyClass` class based on the fields `id` and `name`. You can customize the behavior of these methods by providing additional options to the `@EqualsAndHashCode` annotation.
For example, you can exclude certain fields from the equality check by using the `exclude` parameter:
```java
@EqualsAndHashCode(exclude = "name")
public class MyClass {
// fields and methods
}
```
This will generate the `equals()` and `hashCode()` methods considering all fields except the `name` field.
I hope this helps! Let me know if you have any more questions.
阅读全文