[Java] 68. Hash Code

백하림's avatar
Feb 18, 2025
[Java] 68. Hash Code

1. Hash 개념

notion image

2. Hash 알고리즘

package ex17; public class Ha01 { public static int simpleHash(String key, int tableSize) { int hash = 0; for (char c : key.toCharArray()) { hash += c; // 각 문자의 ASCII 값을 더함 } return hash % tableSize; // 테이블 크기로 나눈 나머지를 해시 값으로 사용 } public static void main(String[] args) { String key1 = "apple"; String key2 = "banana"; int tableSize = 10; // 해시 테이블 크기 System.out.println("apple의 해시 값: " + simpleHash(key1, tableSize)); System.out.println("banana의 해시 값: " + simpleHash(key2, tableSize)); } }
notion image

3. 객체의 주소 값 확인

package ex17; class Animal { } public class Ha02 { public static void main(String[] args) { Animal a1 = new Animal(); Animal a2 = new Animal(); System.out.println(a1.hashCode()); System.out.println(a2.hashCode()); } }
notion image
Share article

harimmon