About the equals () and hashcode () methods

I'm new to java.

This time I created a class called Information, and in it ** ・ address [String] ** ** ・ phone (phone number) [String] ** I made a field called. In addition, we have prepared getters and setters for each item.

Next, in order to create a contact search function, create a Person class, and in it, ** ・ name [String] ** ** ・ birthday Day [int] ** ** ・ information [Information] ** I made a field called, and set the name, birthday and information by the factor of the constructor, and prepared each getter.

Next, I tried to add the following three methods to the Information class. ** ・ toString () ** ** ・ equals () ** ** ・ hashcode () **

At this time, if the information is correct, it means that the address and the telephone number match. Also, toString () now returns all fields as strings.

toString () and equals () have been completed somehow, relying on the information on the net. However, I'm struggling because I don't understand hashcode ().

The code I have written now is below. I think there are many points that cannot be reached other than hashcode (), so I would be grateful if you could comment on those points as well.

Person.class



public class Person {

  private String name;
  private int birthday;
  private Information information;

  public Person(String name, int birthday, Information information) {
    this.name = name;
    this.birthday = birthday;
    this.information = information;
  }

  public String getName() {
    return this.name;
  }

  public int getBirthday() {
    return this.birthday;
  }

  public Information getInformation() {
    return this.information;
  }
}

Information.class



public class Information {

  private String address;
  private String phone;

  public Information() {}

  public String getAddress() {
    return this.address;
  }

  public String getPhone() {
    return this.phone;
  }

  public void setAddress(String address) {
    this.address = address;
  }

  public void setPhone(String phone) {
    this.phone = phone;
  }

	public String toString(){
    String str = address+","+phone;
    return str;
  }
  
  public boolean equals(Object other) {
    
     if (this == other) { 
            return true;
        }

     if (!(other instanceof Information)) { 
            return false;               // 
        }

        Information otherInformation = (Information) other;
     if ((this.address == otherInformation.getAddress()) && (this.phone == otherInformation.getPhone())){ 
            return true;
        }
        return false;
    }
  
	public int hashcode(){
   //I do not know.
  }

}

Recommended Posts

About the equals () and hashcode () methods
About the same and equivalent
[Java] HashCode and equals overrides
About pluck and ids methods
About the relationship between HTTP methods, actions and CRUD
About Java static and non-static methods
== and equals
Understanding equals and hashCode in Java
Think about the differences between functions and methods (in Java)
About the operation of next () and nextLine ()
About the difference between irb and pry
About the mechanism of the Web and HTTP
Find out about instance methods and self
About singular methods
About HttpServlet () methods
About the method
Functions and methods
About the package
About Ruby methods
Think about the combination of Servlet and Ajax
[Rails / ActiveRecord] About the difference between create and create!
About next () and nextLine () of the Scanner class
A note about the Rails and Vue process
[Ruby] Difference between methods with and without self in the class. About class methods and instance methods.
Output about the method # 2
[Ruby] I thought about the difference between each_with_index and each.with_index
About the StringBuilder class
Commentary: About the interface
The comparison of enums is ==, and equals is good [Java]
About the asset pipeline
About the function double-java
[Grails] About the setting area and the setting items of application.yml
About Bean and DI
About classes and instances
About the difference between classes and instances in Ruby
About the ternary operator
About gets and gets.chomp
About Ruby instance methods
About the length method
About calling instance methods
About redirect and forward
About the Kernel module
[Java] About Objects.equals () and Review of String comparisons (== and equals)
About Class loading and initialization when the JVM starts
About encapsulation and inheritance
Ruby variables and methods
About the authenticate method.
[Technical memo] About the advantages and disadvantages of Ruby
About the map method
About Serializable and serialVersionUID
About the ancestors method
[Output] About the database
About the [ruby] operator
About the to_s method.
[Ruby] Questions and verification about the number of method arguments
About the difference between "(double quotation)" and "single quotation" in Ruby
Recommendation of set operation by Java (and understanding of equals and hashCode)
[Ruby] About the difference between 2 dots and 3 dots of range object.
Let's override the difference between == (identity) and equals method (equivalence)
[Java] I thought about the merits and uses of "interface"
About the handling of Null