@EqualsAndHashCode

1, @ Data annotation contain these notes

* @see Getter
* @see Setter
* @see RequiredArgsConstructor
* @see ToString
* @see EqualsAndHashCode
* @see lombok.Value

1. This will generate annotation equals (Object other) and hashCode () method. 
2. It default non-static, non-transitory attributes 
3. exclude some properties may exclude parameter 
4. specify which attributes of the parameters using only the 
5 only attribute which defaults defined in the class and do not call the parent class methods 
6. = true can solve any problems through the callSuper. The method allowed the parent class method call generated.

Another: @Data equivalent collection @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode five annotations.

 

2, @ Data contains the EqualsAndHashCode rewrite the equals method and hashcode

3, EqualsAndHashCode for you inherit parent attribute points to note

Through official documents, you can learn when using @Data notes, the notes have @EqualsAndHashCode, it will have a equals (Object other) and hashCode () method in this class, and does not use the property of the parent class, This leads to possible problems. 
For example, a plurality of the same class have some of the attributes, they define the parent class, exactly id (primary key database) are the parent class, it will have a portion of the subject at the time of comparison, they are not equal, but because lombok automatically generated equals (Object other) and hashCode () method is determined to be equal, thereby causing an error.

The method to fix this problem is simple: 
1. @Getter @Setter @ToString place @Data and custom equals (Object other) and hashCode () method, for example, some primary key is determined based only id equality will suffice. 
2. Use or simultaneously adding @EqualsAndHashCode (callSuper = true) annotations in use @Data.

 

4, the test

@Data
//@EqualsAndHashCode(callSuper=true)
public class UcsAccountLog extends  BaseVO implements Serializable {
 
    private static final long serialVersionUID = 1L;
 
    private String id;
    
}

 

@Data
public class BaseVO {
    public String version;
}

 

/**
     * First, to support the underlying hash table class as the class with the class hash table.
     * Java Object Specification int hashCode () method of class content convention has three:
     * <p>
     * (1) Information as long as the object of the equals method used is not modified, then hashCode method must consistently return an integer of the same, in the same application multiple times, each time execution can be different.
     * <p>
     Digital * (2) If two objects equals method of thought, then each individual object calls hashCode returns must be equal.
     * <p>
     * (3) equals method if two objects are not equal, the return value may be equal to hashCode method, subject to different return different values ​​can improve the performance of the hash table.
     * All override both equals must override hashcode
     * @param args
     */
    public static void main(String[] args) {
        UcsAccountLog ucsAccountLog = new UcsAccountLog();
        ucsAccountLog.setVersion("1");
        ucsAccountLog.setId("2");
        UcsAccountLog ucsAccountLog2 = new UcsAccountLog();
        ucsAccountLog2.setVersion("2");
        ucsAccountLog2.setId("2");
        HashSet set = new HashSet();
        set.add(ucsAccountLog);
        set.add(ucsAccountLog2);
        System.out.println(set.size());//1
        System.out.println (ucsAccountLog.hashCode () == ucsAccountLog2.hashCode ()); // true because hashcode overrides all are equal here hashcode
        // If you do not override the hashcode is based on the value of the memory address
        System.out.println(ucsAccountLog.equals(ucsAccountLog2));//true
        // According to hash
        System.out.println (ucsAccountLog == ucsAccountLog2); // false == comparison is the address of an object
 
    }

 If a subclass 

//@EqualsAndHashCode(callSuper=true)

Do not add this line, the output is true true false and we expect the results do not meet 

 Because @EqualsAndHashCode default does not inherit the parent class that is rewriting hashcode and equals when the field does not contain the value of the parent class, all will compare the field values ​​inside the object itself Obviously this is wrong

Open comment is correct false false false output results

 

 

 

Guess you like

Origin www.cnblogs.com/Andrew520/p/11031623.html