[java] What I did when comparing Lists in my own class

I thought I should have added the equals and hashCode methods, so I could shorten it by using the library of ʻorg.apache.commons`.

Hoge.java


import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
~~abridgement~~
    @Override
    public boolean equals(Object obj) {
        return EqualsBuilder.reflectionEquals(this, obj);
    }
 
    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

If you are using gradle, see below.

build.gradle


// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.0'

Since it was only an old article, I looked up other things and it seemed that the following things could be realized.

Hoge.java


@Override
public int hashCode(){
    return Objects.hash(name, length, children);
}

@Override
public boolean equals(final Object obj){
    if(obj instanceof Bean){
        final Bean other = (Bean) obj;
        return Objects.equals(name, other.name)
            && length == other.length // special handling for primitives
            && Objects.equals(children, other.children);
    } else{
        return false;
    }
}

ʻObjects` was taken care of by null check, but I didn't know that there was such a method.

I feel that it could be done automatically by Eclipse, but I personally think that it is fine if it works.

Recommended Posts

[java] What I did when comparing Lists in my own class
I was trapped when I generated my own class equals and hashCode in Java using IDE
What I did when I converted java to Kotlin
[Java Spring MVC] I want to use DI in my own class
What I learned when building a server in Java
About what I did when creating a .clj file in Clojure
What is a class in Java language (3 /?)
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
I did OpenCV camera calibration in Java
What I did when JSF couldn't display database information in the view
Java method call from RPG (method call in own class)
What I learned in Java (Part 2) What are variables?
What did I prepare for when I entered an Android application development project while developing the Web in Java?
What I learned in Java (Part 3) Instruction execution statement
Compare Lists in Java
I tried to find out what changed in Java 9
[Paiza] I made my own utility for answering questions [Java]
What I learned in Java (Part 4) Conditional branching and repetition
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
What happens when I do new => build => save! In ActiveRecord
What I did when I stumbled on IntelliJ gradle 2.2 → 2.6 environment migration
[AWS] What I'm glad I did with my first deployment
I tried to summarize object orientation in my own way.
What I researched about Java 8
What I researched about Java 6
I made roulette in Java.
What I researched about Java 9
What I researched about Java 7
[Java] What is class inheritance?
[Java basics] What is Class?
I tried metaprogramming in Java
What I researched about Java 5
I want to judge the necessity of testing by comparing the difference of class files when refactoring Java
What I have learned in Java (Part 1) Java development flow and overview
I can't create a Java class with a specific name in IntelliJ
Symbol not found error when new class in another Java file
[Note] What I learned in half a year from inexperienced (Java)
[Note] What I learned in half a year from inexperienced (Java) (1)
What I did when the DB did not start with docker-compose up
What I did in the migration from Spring Boot 1.4 series to 2.0 series
[Note] What I learned in half a year from inexperienced (Java) (3)
What I did in the migration from Spring Boot 1.5 series to 2.0 series
What I often do when I have trouble naming with Java (etc.)
What I thought about when I started migrating from Java to Kotlin
What I don't like when using interface of a function with default arguments in Kotlin from Java
What I checked when I installed Docker Hub in a Windows 10 home environment but it did not start
I sent an email in Java
I created a PDF in Java.
<java> When "EXCEPTION_ACCESS_VIOLATION" appears in awt
Handle your own annotations in Java
I wrote Goldbach's theorem in java
StringBuffer and StringBuilder Class in Java
[Java] What got caught in encapsulation
What I learned with Java Gold
I made an annotation in Java.
I tried using JWT in Java
What I learned with Java Silver
What I researched about Java learning
What I thought when passing the user input value to the Service class
When updating my own application, I seriously thought about the package structure