public static void String_비교() {
String s1 = new String("Hello");
String s2 = new String("Hello");
System.out.println(s1 == s2); // 주소 비교 false
System.out.println(s1.equals(s2)); // 값 비교 true
}
public static void main(String[] args) {
String_비교();
}
package equlas_hashcode;
public class Person {
private PersonId id;
public Person(PersonId id) {
this.id = id;
}
public static class PersonId {
private long value;
public PersonId(long value) {
this.value = value;
}
}
}
public static void 객체_비교() {
Person.PersonId p1 = new Person.PersonId(1L);
Person.PersonId p2 = new Person.PersonId(1L);
Person person1 = new Person(p1);
Person person2 = new Person(p2);
System.out.println(person1 == person2);
System.out.println(person1.equals(person2));
}
public static void main(String[] args) {
객체_비교();
}
package equlas_hashcode;
import java.util.Objects;
public class Person {
private PersonId id;
public Person(PersonId id) {
this.id = id;
}
public static class PersonId {
private long value;
public PersonId(long value) {
this.value = value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof PersonId personId)) return false;
return value == personId.value;
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Person person)) return false;
return Objects.equals(this.id, person.id);
}
}
Objects class
public static void 객체_해쉬코드_찍어보기() {
Person.PersonId p1 = new Person.PersonId(1L);
Person.PersonId p2 = new Person.PersonId(1L);
Person person1 = new Person(p1);
Person person2 = new Person(p2);
System.out.println(person1.hashCode());
System.out.println(person2.hashCode());
}
public static void main(String[] args) {
객체_해쉬코드_찍어보기();
}
public static void 해쉬코드_오버라이딩안하고_리스트_사이즈_재보기() {
List<Person> personList = new ArrayList<>();
Person.PersonId p1 = new Person.PersonId(1L);
Person.PersonId p2 = new Person.PersonId(1L);
Person person1 = new Person(p1);
Person person2 = new Person(p2);
personList.add(person1);
personList.add(person2);
System.out.println(personList.size());
}
public static void main(String[] args) {
해쉬코드_오버라이딩안하고_리스트_사이즈_재보기();
}
public static void 해쉬코드_오버라이딩안하고_세트_사이즈_재보기() {
Set<Person> personList = new HashSet<>();
Person.PersonId p1 = new Person.PersonId(1L);
Person.PersonId p2 = new Person.PersonId(1L);
Person person1 = new Person(p1);
Person person2 = new Person(p2);
personList.add(person1);
personList.add(person2);
System.out.println(personList.size());
}
public static void main(String[] args) {
해쉬코드_오버라이딩안하고_세트_사이즈_재보기();
}
package equlas_hashcode;
import java.util.Objects;
public class Person {
private PersonId id;
public Person(PersonId id) {
this.id = id;
}
public static class PersonId {
private long value;
public PersonId(long value) {
this.value = value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof PersonId personId)) return false;
return value == personId.value;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Person person)) return false;
return Objects.equals(this.id, person.id);
}
@Override
public int hashCode() {
return Objects.hash(id);
}
}
여기서 숙지해야할것은, 우리가 만든 커스텀한 객체의값을 비교할떄, Set에 추가해야할때 eqauls,hashcode를 오버라이딩하였음을 인지 하여야한다.
자바에서 만들어놓은 객체 데이터를 적재할때는 문제가 되지 않는다
public static void 해쉬코드_오버라이딩_했을때와_독립적인_해쉬코드_얻기() {
Set<Person> personList = new HashSet<>();
Person.PersonId p1 = new Person.PersonId(1L);
Person.PersonId p2 = new Person.PersonId(1L);
Person person1 = new Person(p1);
Person person2 = new Person(p2);
System.out.println("person1.hashCode() = " + person1.hashCode());
System.out.println("person2.hashCode() = " + person2.hashCode());
System.out.println("System.identityHashCode(person1) = " + System.identityHashCode(person1));
System.out.println("System.identityHashCode(person2) = " + System.identityHashCode(person2));
}
public static void main(String[] args) {
해쉬코드_오버라이딩_했을때와_독립적인_해쉬코드_얻기();
}