[Java] HashCode and equals overrides

  1. Object HashCode () and equals () image.png image.png as mentioned above, equals () is comparing the addresses of obj. hashCode () is calculated on the native side. In other words, it is calculated on the C / C ++ side.

Then, when comparing ==, it becomes false. image.png

  1. String HashCode () and equals () String inherits object. image.png

String overrides HashCode () and equals (). image.png

image.png The above equals

  1. True if the object has the same address

  2. False if the comparison string class is not String

  3. False if the comparison string class lengths are not the same 4, compare in loop, false if wrong

  4. True when the loop is completed

  5. HashCode () and equals () on your own

    private static class Person { int age; String name;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public String toString() {
        return name + " - " +age;
    }
    
    /**
     * @desc
     */
    @Override
    public boolean equals(Object obj){
        if(obj == null){
            return false;
        }
    
        if(this == obj){
            return true;
        }
    
        if(this.getClass() != obj.getClass()){
            return false;
        }
    
        Person person = (Person)obj;
        return name.equals(person.name) && age==person.age;
    }
    
    @Override
    public int hashCode() {
    	return this.name.hashCode();
    }
    

    }

Sample code https://github.com/YanHengGo/java/tree/master/04_hashcode

Recommended Posts

[Java] HashCode and equals overrides
Understanding equals and hashCode in Java
[Java] Difference between == and equals
C # and Java Overrides Story
== and equals
About the equals () and hashcode () methods
[Java beginner] == operator and equals method
[Java] What are overrides and overloads?
Recommendation of set operation by Java (and understanding of equals and hashCode)
Java and JavaScript
XXE and Java
Java true and false
[Java] String comparison and && and ||
Java --Serialization and Deserialization
[Java] Arguments and parameters
timedatectl and Java TimeZone
[Java] Branch and repeat
[Java] Variables and types
java (classes and instances)
[Java] Overload and override
The comparison of enums is ==, and equals is good [Java]
[Java] About Objects.equals () and Review of String comparisons (== and equals)
Study Java # 2 (\ mark and operator)
Java version 8 and later features
Java equals are pretty unpleasant
[Java] Generics classes and generics methods
Java programming (variables and data)
Java and Iterator Part 1 External Iterator
Java if and switch statements
I was trapped when I generated my own class equals and hashCode in Java using IDE
Java class definition and instantiation
Apache Hadoop and Java 9 (Part 1)
[Java] About String and StringBuilder
☾ Java / Iterative statement and iterative control statement
Java methods and method overloads
java Generics T and? Difference
Advantages and disadvantages of Java
java (conditional branching and repetition)
About Java Packages and imports
[Java] Upload images and base64
Java abstract methods and classes
Java while and for statements
Java encapsulation and getters and setters
[Read Effective Java] Chapter 3 Item 9 "When overriding equals, always override hashCode"
Difference between == operator and equals method
I compared PHP and Java constructors
Differences between "beginner" Java and Kotlin
Use java with MSYS and Cygwin
Distributed tracing with OpenCensus and Java
[Java] Difference between Hashmap and HashTable
Java variable declaration, initialization, and types
Java Excel Insertion and Image Extraction
Install Java and Tomcat with Ansible
AWS SDK for Java 1.11.x and 2.x
[Java] Basic types and instruction notes
Java release date and EOL summary
Java and first-class functions-beyond functional interfaces-
About fastqc of Biocontainers and Java
Java for beginners, expressions and operators 1
Java Primer Series (Variables and Types)
Encoding and Decoding example in Java