자바 Java] Hashtable 예제 소스, 펄의 해쉬(Hash) 구현
그리고 Hashtable이 아닌 HashMap도 있는데, HashMap은 키나 값에 null을 넣을 수 있지만, 쓰레드 세이프(Thread-Safe)가 아니라는 약간의 차이점이 있습니다.
예제 소스 파일명: MyHash.java
class MyHash {
public static void main(String args[]) {
Hashtable<String,String> capitalCity = new Hashtable<String,String>();
capitalCity.put("미국", "워싱턴"); // 해쉬에 아이템을 넣는 작업
capitalCity.put("오스트리아", "빈");
capitalCity.put("프랑스", "Paris");
// 해시 속의 아이템 1개를 화면에 출력하기
System.out.println( capitalCity.get("프랑스") ); // 출력 결과: Paris
}
}
Hashtable capitalCity = new Hashtable();
이렇게 타입을 지정하지 않으면 컴파일시 다음과 같은 경고 비슷한 문구가 나옵니다.
Note: MyHash.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
"키"는 문자열이고, "값"은 정수라면 다음과 같이 합니다.
Hashtable<String, Integer> capitalCity = new Hashtable<String, Integer>();
☞ 자바 | Java






















