Last active
December 18, 2015 16:30
-
-
Save AnnaBoro/3060db2f59caec762962 to your computer and use it in GitHub Desktop.
Person + Address, hash code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson7.hashcodeoverriding; | |
public class Address { | |
private String city; | |
private String street; | |
private int house; | |
public Address() { | |
} | |
@Override | |
public int hashCode() { | |
final int prime = 7; | |
int c = city.hashCode(); | |
int result = 37; | |
result = prime * result + c; | |
c = ((Integer)house).hashCode(); | |
result = prime * result + c; | |
c = street.hashCode(); | |
result = prime * result + c; | |
return result; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
if (obj != null && obj instanceof Address) { | |
Address adr = (Address) obj; | |
return city.equals(adr.getCity()) && street.equals(adr.getStreet()) && house == adr.getHouse(); | |
} | |
return false; | |
} | |
public String getCity() { | |
return city; | |
} | |
public void setCity(String city) { | |
this.city = city; | |
} | |
public String getStreet() { | |
return street; | |
} | |
public void setStreet(String street) { | |
this.street = street; | |
} | |
public int getHouse() { | |
return house; | |
} | |
public void setHouse(int house) { | |
this.house = house; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson7.hashcodeoverriding; | |
public class Launcher2 { | |
public static void main(String[] args) { | |
Person2 p1 = new Person2(); | |
Person2 p2 = new Person2(); | |
Address ad1 = new Address(); | |
Address ad2 = new Address(); | |
p1.setName("Ivan"); | |
p1.setAge(22); | |
p1.setSalary(20); | |
ad1.setCity("Kyiv"); | |
ad1.setStreet("Horkoho"); | |
ad1.setHouse(1); | |
p1.setAddress(ad1); | |
p2.setName("erw"); | |
p2.setAge(22); | |
p2.setSalary(20); | |
ad2.setCity("Kyiv"); | |
ad2.setStreet("Horkoho"); | |
ad2.setHouse(2); | |
p2.setAddress(ad2); | |
System.out.println(p1.hashCode()); | |
System.out.println(p2.hashCode()); | |
System.out.println(p1.equals(p2)); | |
System.out.println(p2.equals(p1)); | |
p1 = p2; | |
System.out.println(p1.hashCode()); | |
System.out.println(p2.hashCode()); | |
System.out.println(p1.equals(p2)); | |
System.out.println(p2.equals(p1)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lesson7.hashcodeoverriding; | |
public class Person2 { | |
private String name; | |
private int age; | |
private long salary; | |
private Address address; | |
public Person2() { | |
} | |
@Override | |
public int hashCode() { | |
final int prime = 7; | |
int c = name.hashCode(); | |
int result = 37; | |
result = prime * result + c; | |
c = ((Integer)age).hashCode(); | |
result = prime * result + c; | |
c = ((Long)salary).hashCode(); | |
result = prime * result + c; | |
c = address.hashCode(); | |
result = prime * result + c; | |
return result; | |
} | |
@Override | |
public boolean equals(Object obj) { | |
Person2 person = (Person2) obj; | |
return getName().equals(person.getName()) && (getAge() == person.getAge()) && (getSalary() == person.getSalary()) | |
&& getAddress().equals(person.getAddress()); | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public int getAge() { | |
return age; | |
} | |
public void setAge(int age) { | |
this.age = age; | |
} | |
public long getSalary() { | |
return salary; | |
} | |
public void setSalary(long salary) { | |
this.salary = salary; | |
} | |
public Address getAddress() { | |
return address; | |
} | |
public void setAddress(Address address) { | |
this.address = address; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment