- Hashtable은 저장되는 순서가 유지 되지 않는다. 또한 null key나 value를 가질 수 없다.
- HashMap은 저장되는 순서가 유지 된다. 또한 null key나 value를 가질 수 있다.
- 둘 다 key 중복시 마지막 저장한 value로 덮어쓴다. 또한 다른 자료구조보다 빠른 검색이 가능하다.
import java.util.HashMap; import java.util.Hashtable; import java.util.Set; public class dailyCode { public static void main(String[] args) { Hashtable<String, String> ht = new Hashtable<String, String>(); ht.put("name", "Alex"); ht.put("age", "21"); //ht.put("age", null); Hashtable can't have null value.
HashMap<String, String> hm = new HashMap<String, String>(); hm.put("name", null); hm.put("age", null); //HashMap can have null value. hm.put("name", "James");
Set<String> keys = ht.keySet(); for(String key : keys) { System.out.println(key + " = " + ht.get(key)); }
keys = hm.keySet(); for(String key : keys) { System.out.println(key + " = " + hm.get(key)); } } } |
'프로그래밍 > JAVA' 카테고리의 다른 글
Find Google IP (InetAddress class Example) (0) | 2016.11.25 |
---|---|
람다 표현식 (Lambda Expression) (0) | 2016.11.24 |
Arrays class Example (0) | 2016.11.23 |
Generics and Wildcard (0) | 2016.11.20 |
arraycopy Example (0) | 2016.11.19 |
scheduleAtFixedRate vs. scheduleWithFixedDelay (0) | 2016.11.18 |
Callback using interface (0) | 2016.11.17 |