728x90
SMALL
- 동일성, 동등성
동등성(equality) 두 객체의 내용이 같나
동일성(identity) 두 객체가 정확히 같은 객체인지
- equals메서드 : 두값이 동등한가?
==연산자 : 두 객체 동일한가? - 해시함수
임의의 길이로 된 데이터를 고정된 길이의 데이터로 매핑하는 함수
h(x) = y
-해시값
-해시코드
-체크섬
public class StickCoffee {
private final String name;
private final long price;
public StickCoffee(String name, long price) {
this.name = name;
this.price = price;
}
// sc1.equals(sc2)
@Override
public boolean equals(Object o) {
// 자기자신인가?
if (this == o)
return true;
// 1. null이면 동등하지 않음
// 2. 클래스가 동일하지 않으면 동등하지 않음
if (o == null || getClass() != o.getClass())
return false;
// 값이 같으면 동등함.
StickCoffee that = (StickCoffee) o;
return price == that.price &&
Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(name, price);
}
@Override
public String toString() {
return "StickCoffee{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
}
public class Application {
public static void main(String[] args) {
/*final StickCoffee sc1 = new StickCoffee("maxim", 1000);
final StickCoffee sc2 = new StickCoffee("maxim", 1000);
System.out.println(sc1 == sc2); // false
System.out.println(sc1.equals("abc")); // true
System.out.println(sc1.getClass());*/
final StickCoffee sc1 = new StickCoffee("maxim", 1000);
final StickCoffee sc2 = new StickCoffee("maxim", 1000);
final StickCoffee sc3 = new StickCoffee("maxim", 1000);
final HashMap<StickCoffee, Integer> map = new HashMap<>();
map.put(sc1, 100);
map.put(sc2, 200);
map.put(sc3, 300);
System.out.println(1081127135 % 128);
System.out.println("++++++++++++++++");
System.out.println(sc1 == sc2); // 동일한 객체인가?
System.out.println(sc1.equals(sc2)); // 동등성 (값이 완전하게 같음?)
System.out.println(sc1.hashCode());
System.out.println(sc2.hashCode());
System.out.println(sc3.hashCode());
System.out.println("=================================\n\n\n\n");
System.out.println(map.get(sc1)); //
System.out.println(map.get(sc2)); //
System.out.println(map.get(sc3)); //
}
}
public class HashMap<K, V> {
private static final int TABLE_SIZE = 128;
private HashEntry<K, V>[] table;
public HashMap() {
table = new HashEntry[TABLE_SIZE];
}
// stick coffee
public V get(K key) {
// 95
final int hashIndex = getHashIndex(key);
final HashEntry<K, V> entry = table[hashIndex];
return entry.getValue();
}
public void put(K key, V value) {
// 95
final int hashIndex = getHashIndex(key);
final HashEntry<K, V> entry = new HashEntry<>(key, value);
table[hashIndex] = entry;
}
private int getHashIndex(K key) {
// sc1.hashCode = -1081127135
// sc2.hashCode = -1081127135
// sc3.hashCode = -1081127135
// 1081127135
return Math.abs(key.hashCode()) % TABLE_SIZE;
}
private static class HashEntry<K, V> {
private final K key;
private final V value;
public HashEntry(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
}
}
728x90
LIST
'JAVA > Java' 카테고리의 다른 글
20201103_27 JVM (0) | 2020.11.03 |
---|---|
20201030_25 이진법, 상속, 클래스 등 (0) | 2020.10.30 |
20201028_23 자바리뷰 (0) | 2020.10.28 |
20201020_19 2차원배열 (0) | 2020.10.20 |
20201020_19 제네릭, static (0) | 2020.10.20 |
댓글