[java] HashMap°ú HashtableÀÇ Â÷ÀÌÁ¡Àº ¹«¾ùÀԴϱî?
¸¹Àº ÇØ´äÀº HashtableÀÌ µ¿±âȵǾî ÀÖÀ½À» ³ªÅ¸³À´Ï´Ù. ½ÇÁ¦·Î ÀÌ°ÍÀº ´ç½ÅÀ» °ÅÀÇ »çÁö ¾Ê½À´Ï´Ù. ¾×¼¼¼ / ¹Â Å×ÀÌŸ ¸Þ¼ÒµåÀÇ µ¿±âÈ´ÂÁöµµ¿¡¼ µ¿½Ã¿¡ Ãß°¡Çϰųª Á¦°ÅÇÏ´Â µÎ °³ÀÇ ½º·¹µå¸¦ ÁßÁöÇÏÁö¸¸ Çö½Ç ¼¼°è¿¡¼´Â Á¾Á¾ Ãß°¡ µ¿±âÈ°¡ ÇÊ¿äÇÕ´Ï´Ù.
¸Å¿ì ÀϹÝÀûÀÎ °ü¿ë±¸´Â "È®ÀÎÇÑ ´ÙÀ½"³Ö´Â °ÍÀÔ´Ï´Ù. Áï, ¸Ê¿¡¼ Ç׸ñÀ» ã°í ÀÌ¹Ì Á¸ÀçÇÏÁö ¾ÊÀ¸¸é Ãß°¡ÇϽʽÿÀ. ÀÌ°ÍÀº Hashtable ¶Ç´Â HashMapÀ» »ç¿ëÇÏ´Â ¾î¶² ¹æ½Ä À¸·Îµç ¿øÀÚ ¿¬»êÀÌ ¾Æ´Õ´Ï´Ù.
µî°¡ ÀûÀ¸·Î µ¿±âÈ µÈ HashMapÀº ´ÙÀ½°ú °°Àº ¹æ¹ýÀ¸·Î ¾òÀ» ¼ö ÀÖ½À´Ï´Ù.
Collections.synchronizedMap(myMap);
±×·¯³ªÀÌ ³í¸®¸¦ ¿Ã¹Ù¸£°Ô ±¸ÇöÇÏ·Á¸é ´ÙÀ½°ú °°Àº Çü½ÄÀÇ Ãß°¡ µ¿±âÈ °¡ ÇÊ¿äÇÕ´Ï´Ù.
synchronized(myMap) {
if (!myMap.containsKey("tomato"))
myMap.put("tomato", "red");
}
HashtableÀÇ ¿£Æ®¸® (¶Ç´Â Collections.synchronizedMap¿¡ ÀÇÇØ Ãëµæ µÈ HashMap)¸¦ ¹Ýº¹Çصµ (ÀÚ), Ãß°¡ÀûÀÎ µ¿±âÈ¿¡ ÀÇÇØ ¸ÊÀÌ º¯°æµÇÁö ¾Ê´Â ÇÑ, thread·ÎºÎÅÍ´Â ¾ÈÀüÇÏÁö ¾Ê½À´Ï´Ù.
ConcurrentMap ÀÎÅÍÆäÀ̽º (¿¹ : ConcurrentHashMap )ÀÇ ±¸ÇöÀº ´ÙÀ½ ConcurrentMap °°Àº ½º·¹µå ¾ÈÀü¼º °Ë»ç - ½ÇÇà - ÀÇ¹Ì Ã¼°è¸¦ Æ÷ÇÔÇÏ¿©ÀÌ Áß ÀϺθ¦ ÇØ°áÇÕ´Ï´Ù.
ConcurrentMap.putIfAbsent(key, value);
HashTable Àº µ¿±âȵǾî ÀÖ½À´Ï´Ù. ´ÜÀÏ ½º·¹µå¿¡¼ »ç¿ëÇÏ´Â °æ¿ì µ¿±âȵÇÁö ¾ÊÀº ¹öÀü ÀÎ docs.oracle.com/javase/7/docs/api/java/util/HashMap.html À» »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù. µ¿±âȵÇÁö ¾ÊÀº °´Ã¼´Â Á¾Á¾ ¾à°£ ´õ È¿°úÀûÀÔ´Ï´Ù. ¿©·¯ ½º·¹µå°¡ µ¿½Ã¿¡ HashMap¿¡ ¾×¼¼½ºÇÏ°í ½º·¹µå Áß Çϳª ÀÌ»óÀÌ ±¸Á¶ÀûÀ¸·Î ¸ÊÀ» ¼öÁ¤ÇÏ´Â °æ¿ì ¿ÜºÎ¿¡¼ µ¿±âÈÇؾßÇÕ´Ï´Ù. À±Àº µ¿±âȵÇÁö ¾ÊÀº ¸ÊÀ» µ¿±âÈ µÈ ¸ÊÀ¸·Î ·¡ÇÎ ÇÒ ¼ö ÀÖ½À´Ï´Ù.
Map m = Collections.synchronizedMap(new HashMap(...));
HashTable¿¡´Â, Å° ¶Ç´Â °ªÀ¸·Î¼ÀÇ null ÀÌ¿ÜÀÇ ¿ÀºêÁ§Æ®¸¸À» Æ÷ÇÔ ÇÒ ¼ö°¡ ÀÖ½À´Ï´Ù. HashMapÀº ÇϳªÀÇ null Å°¿Í null °ªÀ» Æ÷ÇÔ ÇÒ ¼ö ÀÖ½À´Ï´Ù.
Map¿¡ ÀÇÇØ ¸®ÅÏ µÈ ¹Ýº¹ÀÚ´Â ÆнºÆ® ÆнºÆ®ÀÔ´Ï´Ù. ¹Ýº¹ÀÚ°¡ ÀÛ¼ºµÈ ÈÄ ¾ðÁ¦¶óµµ ±¸Á¶Ã¼°¡ ¼öÁ¤µÇ¸é, ¹Ýº¹ÀÚ ÀÚ½ÅÀÇ remove ¸Þ¼Òµå¸¦ ÅëÇÏ´Â °æ¿ì¸¦ Á¦¿ÜÇÏ°í´Â ¹Ýº¹ÀÚ´Â
ConcurrentModificationException
À» ´øÁý´Ï´Ù. µû¶ó¼ µ¿½Ã ¼öÁ¤ÀÌ ÀÌ·ç¾îÁö¸é ¹Ýº¹ÀÚ´Â ¹Ì·¡¿¡ ¿¹ÃøÇÒ ¼ö¾ø´Â ½Ã°£¿¡ ÀÓÀÇÀÇ ºñ °áÁ¤Àû µ¿ÀÛÀ» À§Çè¿¡ ºü¶ß¸®´Â ´ë½Å ºü¸£°í ½Å¼ÓÇÏ°Ô ½ÇÆÐÇÕ´Ï´Ù. HashtableÀÇ Å°¿Í ¿¤¸®¸ÕÆ® ¸Þ¼Òµå¿¡ ÀÇÇØ ¸®ÅÏ µÈ EnumerationÀº fail-fast°¡ ¾Æ´Õ´Ï´Ù.HashTable°ú HashMapÀº Java Collections FrameworkÀÇ ¸â¹öÀÔ´Ï´Ù (Java 2 Ç÷§Æû v1.2 ÀÌÈÄ, HashTableÀº Map ÀÎÅÍÆäÀ̽º¸¦ ±¸ÇöÇϱâ À§ÇØ °³Á¶µÇ¾ú½À´Ï´Ù).
HashTableÀº ·¹°Å½Ã ÄÚµå·Î °£ÁֵǹǷΠ½º·¹µå ¾ÈÀü¼ºÀÌ ³ôÀº µ¿½Ã ±¸ÇöÀÌ ÇÊ¿äÇÑ °æ¿ì Hashtable ´ë½Å ConcurrentHashMap À» »ç¿ëÇÏ´Â °ÍÀÌ ÁÁ½À´Ï´Ù.
HashMap´Â ¿ä¼Ò°¡ ¹ÝȯµÇ´Â ¼ø¼¸¦ º¸ÁõÇÏÁö ¾Ê½À´Ï´Ù. HashTableÀÇ °æ¿ì µ¿ÀÏÇÏÁö¸¸ °°Áö´Â ¾ÊÁö¸¸ È®½ÇÇÏÁö´Â ¾Ê½À´Ï´Ù. ressource¸¦ ãÁö ¸øÇß½À´Ï´Ù.
½º·¹µå µÈ ÀÀ¿ë ÇÁ·Î±×·¥ÀÇ °æ¿ì ¼º´É ¿ä±¸ »çÇ׿¡ µû¶ó ConcurrentHashMap¿¡¼ ¹þ¾î³¯ ¼ö ÀÖ½À´Ï´Ù.
1. Hashmap
°ú HashTable
¸ðµÎ Å°¿Í °ªÀ» ÀúÀåÇÕ´Ï´Ù.
2. Hashmap
Àº ÇϳªÀÇ Å°¸¦ null
·Î ÀúÀåÇÒ ¼ö ÀÖ½À´Ï´Ù. Hashtable
˼ null
ÀúÀåÇÒ ¼ö ¾ø½À´Ï´Ù.
3. HashMap
Àº µ¿±âȵÇÁö ¾ÊÁö¸¸ Hashtable
Àº µ¿±âȵ˴ϴÙ.
4. HashMap
˼ Collection.SyncronizedMap(map)
°ú µ¿±âÈ µÉ ¼ö ÀÖ½À´Ï´Ù.
Map hashmap = new HashMap();
Map map = Collections.SyncronizedMap(hashmap);
ÀÌ¹Ì ¸¹Àº ÁÁÀº ´äº¯ÀÌ °Ô½ÃµÇ¾î ÀÖ½À´Ï´Ù. ¸î °¡Áö »õ·Î¿î Á¡À» Ãß°¡ÇÏ°í ¿ä¾àÇÕ´Ï´Ù.
HashMap
°ú Hashtable
Àº Å°¿Í °ª Çü½ÄÀ¸·Î µ¥ÀÌÅÍ ¸¦ ÀúÀåÇÏ´Â µ¥ »ç¿ëµË´Ï´Ù. µÑ ´Ù °íÀ¯ Å°¸¦ ÀúÀåÇϱâ À§ÇØ ÇØ½Ì ±â¼úÀ» »ç¿ëÇÏ°í ÀÖ½À´Ï´Ù. ±×·¯³ª ¾Æ·¡¿¡ ÁÖ¾îÁø HashMap°ú Hashtable Ŭ·¡½º¿¡´Â ¸¹Àº Â÷ÀÌÁ¡ÀÌ ÀÖ½À´Ï´Ù.
HashMap
1) HashMap
°¡ ºñµ¿±âÀÔ´Ï´Ù. ±×°ÍÀº ½º·¹µå°¡ ¾ÈÀüÇÏÁö ¾ÊÀ¸¸ç ÀûÀýÇÑ µ¿±âÈ Äڵ尡¾ø´Â ¿©·¯ ½º·¹µå°£¿¡ °øÀ¯ µÉ ¼ö ¾ø½À´Ï´Ù.
2) HashMap
Àº ÇϳªÀÇ ³Î (null) Å°¿Í º¹¼öÀÇ ³Î°ªÀ» Çã¿ëÇÕ´Ï´Ù.
3) HashMap
Àº JDK 1.2¿¡¼ ¼Ò°³ µÈ »õ·Î¿î Ŭ·¡½ºÀÔ´Ï´Ù.
4) HashMap
ÀÌ ºü¸¨´Ï´Ù.
5)ÀÌ Äڵ带 È£ÃâÇÏ¿© HashMap
À» µ¿±âÈ µÈ »óÅ·Π¸¸µé ¼ö ÀÖ½À´Ï´Ù. Map m = Collections.synchronizedMap(HashMap);
6) HashMap
Àº Iterator¿¡ ÀÇÇØ Æ®·¡¹ö½ºµË´Ï´Ù.
7) HashMap
Iterator´Â fail-fastÀÔ´Ï´Ù.
8) HashMap
AbstractMap Ŭ·¡½º¸¦ »ó¼Ó¹Þ½À´Ï´Ù.
Hashtable
1) Hashtable
ÀÌ µ¿±âȵ˴ϴÙ. ½º·¹µå·ÎºÎÅÍ ¾ÈÀüÇÏ¸ç ¸¹Àº ½º·¹µå¿Í °øÀ¯ ÇÒ ¼ö ÀÖ½À´Ï´Ù.
2) Hashtable
Àº null Å° ¶Ç´Â °ªÀ» Çã¿ëÇÏÁö ¾Ê½À´Ï´Ù.
3) Hashtable
Àº ·¹°Å½Ã Ŭ·¡½ºÀÔ´Ï´Ù.
4) Hashtable
ÀÌ ´À¸³´Ï´Ù.
5) Hashtable
Àº ³»ºÎÀûÀ¸·Î µ¿±âȵǾîÀÖ¾î¼ ºñµ¿±âÈÇÒ ¼ö ¾ø½À´Ï´Ù.
6) Hashtable
Àº Enumerator¿Í Iterator¿¡ ÀÇÇØ Æ®·¡¹ö½ºµË´Ï´Ù.
7) Hashtable
¿°ÅÀÚ´Â ½ÇÆÐÇÏÁö ¾Ê½À´Ï´Ù.
8) Hashtable
Dictionary Ŭ·¡½º¸¦ »ó¼Ó¹Þ½À´Ï´Ù.
Java¿¡¼ HashMap °ú HashtableÀÇ Â÷ÀÌÁ¡ :
1) ½º·¹µå ¾ÈÀü
- HashTableÀº ³»ºÎÀûÀ¸·Î µ¿±âȵ˴ϴÙ.
- µû¶ó¼ ´ÙÁß ½º·¹µå ÀÀ¿ë ÇÁ·Î±×·¥¿¡¼ HashTableÀ» »ç¿ëÇÏ´Â °ÍÀÌ ¸Å¿ì ¾ÈÀüÇÕ´Ï´Ù.
- HashMap°¡ ³»ºÎÀûÀ¸·Î µ¿±âȵÇÁö ¾ÊÀº °÷.
- µû¶ó¼ ¿ÜºÎ µ¿±âȾøÀÌ ´ÙÁß ½º·¹µå ÀÀ¿ë ÇÁ·Î±×·¥¿¡¼ HashMapÀ» »ç¿ëÇÏ´Â °ÍÀº ¾ÈÀüÇÏÁö ¾Ê½À´Ï´Ù.
- Collections.synchronizedMap () ¸Þ¼µå¸¦ »ç¿ëÇÏ¿© HashMapÀ» ¿ÜºÎ¿¡¼ µ¿±âÈ ÇÒ ¼ö ÀÖ½À´Ï´Ù.
2) »ó¼Ó µÈ
- HashMap°ú HashTable ¸ðµÎ Map ÀÎÅÍÆäÀ̽º¸¦ ±¸ÇöÇÏÁö¸¸ µÎ °³ÀÇ ´Ù¸¥ Ŭ·¡½º¸¦ È®ÀåÇÕ´Ï´Ù.
- HashMapÀº AbstractMap Ŭ·¡½º¸¦ È®ÀåÇÕ´Ï´Ù. HashTableÀº javaÀÇ ·¹°Å½Ã Ŭ·¡½º ÀÎ Dictionary Ŭ·¡½º¸¦ È®ÀåÇÕ´Ï´Ù.
3) Null Å°¿Í Null °ª
- HashMapÀº ÃÖ´ë ÇϳªÀÇ ³Î (NULL) Å°¿Í ÀÓÀÇÀÇ ¼öÀÇ ³Î°ªÀ» Çã¿ëÇÕ´Ï´Ù.
- HashTableÀº ÇϳªÀÇ null Å°¿Í null °ªÁ¶Â÷ Çã¿ëÇÏÁö ¾Ê½À´Ï´Ù.
4) ¼øȸ
- HashMapÀº HashMapÀÇ ¿ä¼Ò¸¦ °¡·Î Áö¸£´Â µ¥ »ç¿ëµÇ´Â Iterator¸¸À» ¹ÝȯÇÕ´Ï´Ù.
- HashTableÀº HashTableÀÇ ¿ä¼Ò¸¦ Ž»öÇÏ´Â µ¥ »ç¿ëÇÒ ¼öÀÖ´Â Iterator ¹× EnumerationÀ» ¹ÝȯÇÕ´Ï´Ù.
5) ÆäÀÏ ¼¼ÀÌÇÁ ´ë ÆäÀÏ ¼¼ÀÌÇÁ
- HashMap¿¡ ÀÇÇØ ¸®ÅÏ µÈ Iterator´Â, º»ÁúÀûÀ¸·Î Æä¾î ÆнºÆ®ÀÔ´Ï´Ù. Áï, iterator ÀÚ½ÅÀÇ remove () ¸Þ¼Òµå ÀÌ¿ÜÀÇ IteratorÀÇ ÀÛ¼º ÈÄ¿¡ HashMap°¡ º¯°æµÇ¾úÀ» °æ¿ì, ConcurrentModificationException¸¦ ThrowÇÕ´Ï´Ù.
- ÇÑÆí, HashTable¿¡ ÀÇÇØ ¸®ÅÏ µÈ EnumerationÀº º»ÁúÀûÀ¸·Î fail-safeÀÔ´Ï´Ù. Áï, EnumerationÀ» »ý¼º ÇÑ ÈÄ HashTableÀ» ¼öÁ¤ÇÏ¸é ¿¹¿Ü°¡ ¹ß»ýÇÏÁö ¾Ê½À´Ï´Ù.
6) ¼º´É
- HashTableÀº ³»ºÎÀûÀ¸·Î µ¿±âȵDZ⠶§¹®¿¡ HashTableÀ» HashMapº¸´Ù ¾à°£ ´À¸®°Ô ¸¸µì´Ï´Ù.
7) ·¹°Å½Ã ¼ö¾÷
- HashTableÀº ·¹°Å½Ã Ŭ·¡½ºÀÔ´Ï´Ù.
- ±×°ÍÀº °ÅÀÇ ºñÃßõÀ¸·Î ÀÎÇÑ °ÍÀ¸·Î °£Áֵ˴ϴÙ.
- JDK 1.5ºÎÅÍ ConcurrentHashMapÀº HashTableº¸´Ù ´õ ³ªÀº ¿É¼ÇÀ¸·Î °£Áֵ˴ϴÙ.
8) Member of Java Collection Framework
- HashMapÀº JDK 1.2¿¡ óÀ½ ¼Ò°³ µÈ ½ÃÁ¡ºÎÅÍ Java Collection FrameworkÀÇ ¸â¹öÀÔ´Ï´Ù.
- ±×·¯³ª JDK 1.2 ÀÌÀü¿¡´Â HashTableÀÌÀÖ¾ú½À´Ï´Ù. JDK 1.2ºÎÅÍ Map ÀÎÅÍÆäÀ̽º¸¦ ±¸ÇöÇÏ¿© Ä÷º¼Ç ÇÁ·¹ÀÓ ¿öÅ©ÀÇ ¸â¹ö·Î ¸¸µé¾ú½À´Ï´Ù.
ºñ ½º·¹µå ÀÀ¿ë ÇÁ·Î±×·¥¿¡ ´õ È¿À²ÀûÀÎ ¹æ¹ýÀº ¹«¾ùÀԴϱî?
HashtableÀº µ¿±âȵÇÁö¸¸ HashtableÀº µ¿±âȵÇÁö ¾Ê½À´Ï´Ù.
ÀÌ°ÍÀº µ¿±âȵÇÁö ¾ÊÀº °´Ã¼°¡ ÀϹÝÀûÀ¸·Î µ¿±âÈ µÈ °´Ã¼º¸´Ù Àß ¼öÇàµÇ±â ¶§¹®¿¡ ºñ ½º·¹µå ÀÀ¿ë ÇÁ·Î±×·¥ ¿¡¼ HashMapÀ» ´õ Àß ¸¸µì´Ï´Ù.
HashTable Àº ´õ ÀÌ»ó »ç¿ëÇؼ´Â ¾ÈµÇ´Â JDKÀÇ ·¹°Å½Ã Ŭ·¡½ºÀÔ´Ï´Ù. ConcurrentHashMap »ç¿ëÇÏ¿© ±×°ÍÀ» ´ëüÇϽʽÿÀ. ½º·¹µå ¾ÈÀü¼ºÀÌ ÇÊ¿äÇÏÁö ¾ÊÀº °æ¿ì HashMap À» »ç¿ëÇϽʽÿÀ. HashMap Àº ½º·¹µå ¼¼ÀÌÇÁ´Â ¾Æ´ÏÁö¸¸ ´õ ºü¸¥ ¸Þ¸ð¸®¸¦ »ç¿ëÇÕ´Ï´Ù.
HashMaps gives you freedom of synchronization and debugging is lot more easier
ÀÌ Áú¹®Àº Á¾Á¾ ÀÎÅͺ信¼ Èĺ¸ÀÚ°¡ ¼öÁý ¼ö¾÷ÀÇ ¿Ã¹Ù¸¥ »ç¿ë¹ýÀ» ÀÌÇØÇÏ°í ÀÖ´ÂÁö È®ÀÎÇÏ°í »ç¿ë °¡´ÉÇÑ ´ëü ÇØ°áÃ¥À» ¾Ë°í ÀÖ´ÂÁö È®ÀÎÇÕ´Ï´Ù.
- HashMap Ŭ·¡½º´Â Hashtable°ú °ÅÀÇ µ¿ÀÏÇÏÁö¸¸, µ¿±âȵÇÁö ¾Ê°í nullÀ» Çã¿ëÇÑ´Ù´Â Á¡¸¸ ´Ù¸¨´Ï´Ù. (HashMapÀº null °ªÀ» Å°¿Í °ªÀ¸·Î Çã¿ëÇÏÁö¸¸ HashtableÀº nullÀ» Çã¿ëÇÏÁö ¾Ê½À´Ï´Ù.)
- HashMap´ÂÁöµµÀÇ ¼ø¼°¡ ½Ã°£ÀÌ Áö³²¿¡ µû¶ó ÀÏÁ¤ÇÏ°Ô À¯Áö µÉ °ÍÀ̶ó°í º¸ÀåÇÏÁö ¾Ê½À´Ï´Ù.
- HashMapÀº µ¿±âȵÇÁö ¾ÊÀº ¹Ý¸é HashMapÀº µ¿±âȵÇÁö ¾Ê½À´Ï´Ù.
- HashMapÀÇ Iterator´Â HashtableÀÇ ¿°ÅÀÚ°¡¾ø´Â µ¿¾ÈÀº ¾ÈÀüÇÕ´Ï´Ù. ´Ù¸¥ Thread°¡ Iterator ÀÚ½ÅÀÇ remove () ¸Þ¼Òµå¸¦ Á¦¿ÜÇÑ ¿ä¼Ò¸¦ Ãß°¡Çϰųª Á¦°ÅÇÏ¿© ±¸Á¶ÀûÀ¸·Î ¸ÊÀ» ¼öÁ¤Çϸé ConcurrentModificationExceptionÀ» ´øÁý´Ï´Ù. ±×·¯³ª ÀÌ°ÍÀº º¸Àå µÈ ÇൿÀÌ ¾Æ´Ï¸ç ÃÖ¼±ÀÇ ³ë·ÂÀ» ±â¿ï¿© JVM¿¡ ÀÇÇØ ¼öÇàµË´Ï´Ù.
¸î¸î Áß¿äÇÑ ±â°£¿¡ ÁÖ
- µ¿±âÈ µÊÀº ÇϳªÀÇ ½º·¹µå ¸¸ÀÌ ÇÑ ½ÃÁ¡¿¡¼ Çؽà Å×À̺íÀ» ¼öÁ¤ÇÒ ¼ö ÀÖÀ½À» ÀǹÌÇÕ´Ï´Ù. ±âº»ÀûÀ¸·Î, Çؽà Å×ÀÌºí¿¡¼ ¾÷µ¥ÀÌÆ®¸¦ ¼öÇàÇϱâ Àü¿¡ ¸ðµç ½º·¹µå°¡ °´Ã¼¿¡ ´ëÇÑ Àá±ÝÀ» ȹµæÇؾßÇÏ°í ´Ù¸¥ ½º·¹µå´Â Àá±ÝÀ» ÇØÁ¦ ÇÒ ¶§±îÁö ´ë±âÇØ¾ß ÇÔÀ» ÀǹÌÇÕ´Ï´Ù.
- ÆäÀÏ ¼¼ÀÌÇÁ´Â ¹Ýº¹ÀÚÀÇ ÄÁÅؽºÆ®¿Í °ü·ÃÀÌ ÀÖ½À´Ï´Ù. ¹Ýº¹ÀÚ°¡ ÄÝ·º¼Ç °´Ã¼·Î ÀÛ¼ºµÇ¾î ´Ù¸¥ thread°¡ ¡¸±¸Á¶ÀûÀ¸·Î¡¹ÄÝ·º¼Ç °´Ã¼¸¦ º¯°æÇÏ·Á°íÇϸé (ÀÚ), º´Çà º¯°æ ¿¹¿Ü°¡ ThrowµË´Ï´Ù. ±×·¯³ª "±¸Á¶ÀûÀ¸·Î"Ä÷º¼ÇÀ» ¼öÁ¤ÇÏÁö ¾ÊÀ¸¹Ç·Î ´Ù¸¥ ½º·¹µå°¡ "set"¸Þ¼µå¸¦ È£Ãâ ÇÒ ¼ö ÀÖ½À´Ï´Ù. ´Ù¸¸, ¡¸set¡¹¸¦ È£ÃâÇϱâ Àü¿¡, Ä÷º¼ÇÀÌ ±¸Á¶ÀûÀ¸·Î º¯°æµÇ¾úÀ» °æ¿ì´Â, ¡¸IllegalArgumentException¡¹°¡ ThrowµË´Ï´Ù.
- ±¸Á¶ÀûÀ¸·Î ¼öÁ¤À̶õÁöµµÀÇ ±¸Á¶¸¦ È¿°úÀûÀ¸·Î º¯°æÇÒ ¼öÀÖ´Â ¿ä¼Ò¸¦ »èÁ¦Çϰųª »ðÀÔÇÏ´Â °ÍÀ» ÀǹÌÇÕ´Ï´Ù.
HashMapÀº ´ÙÀ½°ú °°ÀÌ µ¿±âÈ ÇÒ ¼ö ÀÖ½À´Ï´Ù.
Map m = Collections.synchronizeMap(hashMap);
Map´Â Enumeration °´Ã¼¸¦ ÅëÇØ ¹Ýº¹À» Á÷Á¢ Áö¿øÇÏ´Â ´ë½Å Collection ºä¸¦ Á¦°øÇÕ´Ï´Ù. Ä÷º¼Ç ºä´ÂÀÌ ¼½¼ÇÀÇ µÞºÎºÐ¿¡¼ ¼³¸íÇÏ´Â °Íó·³ ÀÎÅÍÆäÀ̽ºÀÇ Ç¥Çö·ÂÀ» Å©°Ô Çâ»ó½Ãŵ´Ï´Ù. Áöµµ¸¦ »ç¿ëÇϸé Å°, °ª ¶Ç´Â Å° - °ª ½ÖÀ» ¹Ýº¹ ÇÒ ¼ö ÀÖ½À´Ï´Ù. HashtableÀº ¼¼ ¹ø° ¿É¼ÇÀ» Á¦°øÇÏÁö ¾Ê½À´Ï´Ù. MapÀº ¹Ýº¹µÇ´Â µ¿¾È ¿£Æ®¸®¸¦ Á¦°ÅÇÏ´Â ¾ÈÀüÇÑ ¹æ¹ýÀ» Á¦°øÇÕ´Ï´Ù. HashtableÀº ±×·¸Áö ¾Ê¾Ò½À´Ï´Ù. ¸¶Áö¸·À¸·Î MapÀº Hashtable ÀÎÅÍÆäÀ̽ºÀÇ »ç¼ÒÇÑ °áÁ¡À» ¼öÁ¤ÇÕ´Ï´Ù. Hashtable¿¡´Â contains¶ó´Â ¸Þ¼µå°¡ Àִµ¥, Hashtable¿¡ ÁöÁ¤µÈ °ªÀÌ µé¾î ÀÖÀ¸¸é true¸¦ ¹ÝȯÇÕ´Ï´Ù. ±× À̸§ÀÌ ÁÖ¾îÁö¸é (ÀÚ), Hashtable°¡ HashtableÀÇ ÁÖ ¾×¼¼½º±â±¸À̱â (À§ÇØ) ¶§¹®¿¡, Hashtable°¡ ÁöÁ¤µÈ Å°¸¦ Æ÷ÇÔÇÑ °æ¿ì¿¡,ÀÌ ¸Þ¼Òµå°¡ true¸¦ µ¹·Á ÁÙ °ÍÀ» ±â´ëÇÒ ¼ö ÀÖ½À´Ï´Ù. Map ÀÎÅÍÆäÀ̽º´Â containsValue ¸Þ¼ÒµåÀÇ À̸§À» º¯°æÇÔÀ¸·Î½á È¥¶õÀÇ ±Ù¿øÀ» Á¦°ÅÇÕ´Ï´Ù. ¶ÇÇÑ ÀÌ°ÍÀº ÀÎÅÍÆäÀ̽ºÀÇ ÀÏ°ü¼ºÀ» Çâ»ó½Ãŵ´Ï´Ù - containsValue parallel contains containsKey.
ÀÌ µµÇ¥¸¦º¸½Ê½Ã¿À. HashMap ¹× Hashtable°ú ÇÔ²² ´Ù¸¥ µ¥ÀÌÅÍ ±¸Á¶ °£ÀÇ ºñ±³¸¦ Á¦°øÇÕ´Ï´Ù. ºñ±³´Â Á¤È®ÇÏ°í ¸íÈ®Çϸç ÀÌÇØÇϱ⠽±½À´Ï´Ù.
Çؽà Å×À̺í°ú Çؽà ¸ÊÀÇ ¶Ç ´Ù¸¥ ÁÖ¿äÇÑ Â÷ÀÌÁ¡Àº HashMapÀÇ Iterator´Â fail-fastÀÌÁö¸¸ HashtableÀÇ ¿°ÅÀÚ´Â Á¸ÀçÇÏÁö ¾Ê°í ´Ù¸¥ Thread°¡ Iterator ÀÚüÀÇ remove () ¸Þ¼Òµå¸¦ Á¦¿ÜÇÑ ¸ðµç ¿ä¼Ò¸¦ Ãß°¡ ¶Ç´Â Á¦°ÅÇÏ¿© ±¸Á¶ÀûÀ¸·Î ¼öÁ¤Çϸé ConcurrentModificationExceptionÀ» ´øÁø´Ù´Â Á¡ÀÔ´Ï´Ù. ±×·¯³ª ÀÌ°ÍÀº º¸Àå µÈ ÇൿÀÌ ¾Æ´Ï¸ç JVMÀÌ ÃÖ¼±ÀÇ ³ë·ÂÀ» ´ÙÇÒ °ÍÀÔ´Ï´Ù. "
³» Ãâó : http://javarevisited.blogspot.com/2010/10/difference-between-hashmap-and.html
Collection
¶§¶§·Î ÄÁÅ×À̳ʶó°íµµ ÇÔ)Àº ´Ü¼øÈ÷ ¿©·¯ ¿ä¼Ò¸¦ ´ÜÀÏ ´ÜÀ§·Î ±×·ìÈÇÏ´Â °´Ã¼ÀÔ´Ï´Ù. Collection
Àº Áý°è µ¥ÀÌÅ͸¦ ÀúÀå, °Ë»ö, Á¶ÀÛ ¹× Àü´ÞÇÏ´Â µ¥ »ç¿ëµË´Ï´Ù. Ä÷º¼Ç ÇÁ·¹ÀÓ ¿öÅ© W ´Â Ä÷º¼ÇÀ» ³ªÅ¸³»°í Á¶ÀÛÇϱâÀ§ÇÑ ÅëÀÏ µÈ ¾ÆÅ°ÅØóÀÔ´Ï´Ù.
HashMap
JDK1.2
¿Í Hashtable JDK1.0
Àº ¸ðµÎ <Key, Value>
½ÖÀ¸·Î Ç¥ÇöµÇ´Â °´Ã¼ ±×·ìÀ» ³ªÅ¸³»´Â µ¥ »ç¿ëµË´Ï´Ù. °¢ <Key, Value>
½ÖÀ» Entry
°´Ã¼¶ó°íÇÕ´Ï´Ù. Entry Ä÷º¼ÇÀº HashMap
°ú Hashtable
ÀÇ °´Ã¼¿¡ ÀÇÇØ ÂüÁ¶µË´Ï´Ù. Ä÷º¼ÇÀÇ Å°´Â °íÀ¯Çϰųª °íÀ¯ÇؾßÇÕ´Ï´Ù. [¸ÅÇÎ µÈ °ª¿¡ ƯÁ¤ Å°¸¦ °Ë»öÇÏ´Â µ¥ »ç¿ëµË´Ï´Ù. Ä÷º¼ÇÀÇ °ªÀ» º¹Á¦ ÇÒ ¼ö ÀÖ½À´Ï´Ù.]
« ¼öÆÛ Å¬·¡½º, ·¹°Å½Ã ¹× ÄÝ·º¼Ç Framework ¸â¹ö
HashtableÀº Dictionary Ŭ·¡½ºÀÇ ¼ºê Ŭ·¡½º ÀÎ JDK1.0
¿¡¼ ¼Ò°³ µÈ ·¹°Å½Ã Ŭ·¡½ºÀÔ´Ï´Ù. JDK1.2
HashtableÀº Map ÀÎÅÍÆäÀ̽º ¸¦ ±¸ÇöÇÏ¿© Ä÷º¼Ç ÇÁ·¹ÀÓ ¿öÅ©ÀÇ ¸â¹ö°¡µÇµµ·Ï ´Ù½Ã ¿£Áö´Ï¾î¸µµÇ¾ú½À´Ï´Ù. HashMapÀº JDK1.2
¿¡¼ óÀ½ ¼Ò°³ µÈ ½ÃÁ¡ºÎÅÍ Java Collection FrameworkÀÇ ¸â¹öÀÔ´Ï´Ù. HashMap´Â AbstractMap Ŭ·¡½ºÀÇ ¼ºê Ŭ·¡½ºÀÔ´Ï´Ù.
public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, Cloneable, Serializable { ... }
public class HashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, Cloneable, Serializable { ... }
« Ãʱ⠿뷮 ¹× ºÎÇÏÀ²
¿ë·®Àº Çؽà Å×À̺íÀÇ ¹öŶ ¼öÀ̸ç Ãʱ⠿뷮Àº ´Ü¼øÈ÷ Çؽà Å×À̺íÀ» ¸¸µé ¶§ÀÇ ¿ë·®ÀÔ´Ï´Ù. Çؽà Å×À̺íÀº ¿·Á ÀÖ½À´Ï´Ù. " hash
collision
"ÀÇ °æ¿ì ´ÜÀÏ ¹öŶÀº ¿©·¯ Ç׸ñÀ» ÀúÀåÇϹǷΠ¼øÂ÷ÀûÀ¸·Î °Ë»öÇؾßÇÕ´Ï´Ù. ·Îµå °è¼ö´Â Çؽà Å×À̺íÀÇ ¿ë·®ÀÌ ÀÚµ¿À¸·Î Áõ°¡Çϱâ Àü¿¡ Çؽà Å×À̺íÀÌ ¾ó¸¶³ª ä¿öÁö´ÂÁö ÃøÁ¤ÇÕ´Ï´Ù.
HashMap´Â µðÆúÆ®ÀÇ Ãʱ⠿뷮 (16) °ú µðÆúÆ®ÀÇ ºÎÇÏ °è¼ö (0.75)·Î ÇÏ´ÃÀÇ Çؽà Å×À̺íÀ» ±¸ÃàÇÕ´Ï´Ù. Where as Hashtable constructs empty hashtable with a default initial capacity (11) and load factor/fill ratio (0.75).
« Structural modification in case of hash collision
HashMap
, Hashtable
in case of hash collisions they store the map entries in linked lists. From Java8 for HashMap
if hash bucket grows beyond a certain threshold, that bucket will switch from linked list of entries to a balanced tree
. which improve worst-case performance from O(n) to O(log n). While converting the list to binary tree, hashcode is used as a branching variable. If there are two different hashcodes in the same bucket, one is considered bigger and goes to the right of the tree and other one to the left. But when both the hashcodes are equal, HashMap
assumes that the keys are comparable, and compares the key to determine the direction so that some order can be maintained. It is a good practice to make the keys of HashMap
comparable . On adding entries if bucket size reaches TREEIFY_THRESHOLD = 8
convert linked list of entries to a balanced tree, on removing entries less than TREEIFY_THRESHOLD
and at most UNTREEIFY_THRESHOLD = 6
will reconvert balanced tree to linked list of entries. Java 8 SRC , stackpost
« Collection-view iteration, Fail-Fast and Fail-Safe
+--------------------+-----------+-------------+
| | Iterator | Enumeration |
+--------------------+-----------+-------------+
| Hashtable | fail-fast | safe |
+--------------------+-----------+-------------+
| HashMap | fail-fast | fail-fast |
+--------------------+-----------+-------------+
| ConcurrentHashMap | safe | safe |
+--------------------+-----------+-------------+
Iterator
is a fail-fast in nature. ie it throws ConcurrentModificationException if a collection is modified while iterating other than it's own remove() method. Where as Enumeration
is fail-safe in nature. It doesn't throw any exceptions if a collection is modified while iterating.
According to Java API Docs, Iterator is always preferred over the Enumeration.
NOTE: The functionality of Enumeration interface is duplicated by the Iterator interface. In addition, Iterator adds an optional remove operation, and has shorter method names. New implementations should consider using Iterator in preference to Enumeration.
In Java 5 introduced ConcurrentMap Interface : ConcurrentHashMap
- a highly concurrent, high-performance ConcurrentMap
implementation backed by a hash table. This implementation never blocks when performing retrievals and allows the client to select the concurrency level for updates. It is intended as a drop-in replacement for Hashtable
: in addition to implementing ConcurrentMap
, it supports all of the "legacy" methods peculiar to Hashtable
.
Each
HashMapEntry
s value is volatile thereby ensuring fine grain consistency for contended modifications and subsequent reads; each read reflects the most recently completed updateIterators and Enumerations are Fail Safe - reflecting the state at some point since the creation of iterator/enumeration; this allows for simultaneous reads and modifications at the cost of reduced consistency. They do not throw ConcurrentModificationException. However, iterators are designed to be used by only one thread at a time.
Like
Hashtable
but unlikeHashMap
, this class does not allow null to be used as a key or value.
public static void main(String[] args) {
//HashMap<String, Integer> hash = new HashMap<String, Integer>();
Hashtable<String, Integer> hash = new Hashtable<String, Integer>();
//ConcurrentHashMap<String, Integer> hash = new ConcurrentHashMap<>();
new Thread() {
@Override public void run() {
try {
for (int i = 10; i < 20; i++) {
sleepThread(1);
System.out.println("T1 :- Key"+i);
hash.put("Key"+i, i);
}
System.out.println( System.identityHashCode( hash ) );
} catch ( Exception e ) {
e.printStackTrace();
}
}
}.start();
new Thread() {
@Override public void run() {
try {
sleepThread(5);
// ConcurrentHashMap traverse using Iterator, Enumeration is Fail-Safe.
// Hashtable traverse using Enumeration is Fail-Safe, Iterator is Fail-Fast.
for (Enumeration<String> e = hash.keys(); e.hasMoreElements(); ) {
sleepThread(1);
System.out.println("T2 : "+ e.nextElement());
}
// HashMap traverse using Iterator, Enumeration is Fail-Fast.
/*
for (Iterator< Entry<String, Integer> > it = hash.entrySet().iterator(); it.hasNext(); ) {
sleepThread(1);
System.out.println("T2 : "+ it.next());
// ConcurrentModificationException at java.util.Hashtable$Enumerator.next
}
*/
/*
Set< Entry<String, Integer> > entrySet = hash.entrySet();
Iterator< Entry<String, Integer> > it = entrySet.iterator();
Enumeration<Entry<String, Integer>> entryEnumeration = Collections.enumeration( entrySet );
while( entryEnumeration.hasMoreElements() ) {
sleepThread(1);
Entry<String, Integer> nextElement = entryEnumeration.nextElement();
System.out.println("T2 : "+ nextElement.getKey() +" : "+ nextElement.getValue() );
//java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextNode
// at java.util.HashMap$EntryIterator.next
// at java.util.Collections$3.nextElement
}
*/
} catch ( Exception e ) {
e.printStackTrace();
}
}
}.start();
Map<String, String> unmodifiableMap = Collections.unmodifiableMap( map );
try {
unmodifiableMap.put("key4", "unmodifiableMap");
} catch (java.lang.UnsupportedOperationException e) {
System.err.println("UnsupportedOperationException : "+ e.getMessage() );
}
}
static void sleepThread( int sec ) {
try {
Thread.sleep( 1000 * sec );
} catch (InterruptedException e) {
e.printStackTrace();
}
}
« Null Keys And Null Values
HashMap
allows maximum one null key and any number of null values. Where as Hashtable
doesn't allow even a single null key and null value, if the key or value null is then it throws NullPointerException. Example
« Synchronized, Thread Safe
Hashtable
is internally synchronized. Therefore, it is very much safe to use Hashtable
in multi threaded applications. Where as HashMap
is not internally synchronized. Therefore, it is not safe to use HashMap
in multi threaded applications without external synchronization. You can externally synchronize HashMap
using Collections.synchronizedMap()
method.
« Performance
As Hashtable
is internally synchronized, this makes Hashtable
slightly slower than the HashMap
.
@See
HashTable ¹× HashMaps¿¡´Â 5 °¡Áö ±âº» Â÷ÀÌ°¡ ÀÖ½À´Ï´Ù.
- Áöµµ¸¦ »ç¿ëÇϸé Å°, °ª ¹× µÎ Å° - °ª ½ÖÀ» ¹Ýº¹ÇÏ°í °Ë»ö ÇÒ ¼ö ÀÖ½À´Ï´Ù. ¿©±â¼ HashTable¿¡´ÂÀÌ ¸ðµç ±â´ÉÀÌ ¾ø½À´Ï´Ù.
- Hashtable¿¡´Â contains ()¶ó´Â ÇÔ¼ö°¡ Àִµ¥, ÀÌ°ÍÀº ¸Å¿ì È¥¶õ ½º·´´Ù. containsÀÇ Àǹ̰¡ ¾à°£ ´Ù¸£±â ¶§¹®¿¡. ±×°ÍÀÌ Å°¸¦ Æ÷ÇÔÇÏ°í Àְųª °ªÀ» °¡Áö°í ÀÖ´Ù´Â °ÍÀ» ÀǹÌÇմϱî? ÀÌÇØÇϱⰡ ¾î·Æ´Ù. Áöµµ¿¡¼¿Í ¸¶Âù°¡Áö·Î ContainsKey () ¹× ContainsValue () ÇÔ¼öµµ ¸Å¿ì ÀÌÇØÇϱ⠽±½À´Ï´Ù.
- Çؽà ¸Ê¿¡¼´Â ¹Ýº¹ÇÏ¸é¼ ¿ä¼Ò¸¦ ¾ÈÀüÇÏ°Ô Á¦°Å ÇÒ ¼ö ÀÖ½À´Ï´Ù. Çؽà Å×ÀÌºí¿¡¼´Â ºÒ°¡´ÉÇÕ´Ï´Ù.
- HashTables´Â ±âº»ÀûÀ¸·Î µ¿±âȵǹǷΠ¿©·¯ ½º·¹µå¿¡¼ ½±°Ô »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù. HashMaps°¡ ±âº»ÀûÀ¸·Î µ¿±âȵÇÁö ¾Ê¾ÒÀ¸¹Ç·Î ´ÜÀÏ ½º·¹µå¿¡¼¸¸ »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù. ±×·¯³ª Collections util Ŭ·¡½ºÀÇ synchronizedMap (Map m) ÇÔ¼ö¸¦ »ç¿ëÇÏ¿© HashMapÀ» µ¿±âÈÇϵµ·Ï º¯È¯ ÇÒ ¼ö ÀÖ½À´Ï´Ù.
- HashTableÀº null Å° ¶Ç´Â null °ªÀ» Çã¿ëÇÏÁö ¾Ê½À´Ï´Ù. HashMapÀº ÇϳªÀÇ null Å°¿Í ¿©·¯ °³ÀÇ null °ªÀ» Çã¿ëÇÕ´Ï´Ù.
HashMap
: Çؽà Äڵ带 »ç¿ëÇÏ¿© ¹è¿À» À妽ÌÇÏ´Â Map
ÀÎÅÍÆäÀ̽ºÀÇ ±¸ÇöÀÔ´Ï´Ù. Hashtable
: ¾È³çÇϼ¼¿ä. 1998 ³â¿¡ ÀüÈÇß½À´Ï´Ù. ±×µéÀº ±×µéÀÇ ÄÝ·º¼Ç API¸¦ µÇµ¹¸®±â¸¦ ¿øÇÕ´Ï´Ù.
ÁøÁöÇÏ°Ô, ´ç½ÅÀº Hashtable
¿¡¼ ¸Ö¸® ¶³¾îÁ®ÀÖ´Â °ÍÀÌ ÁÁ½À´Ï´Ù. ´ÜÀÏ ½º·¹µå ÀÀ¿ë ÇÁ·Î±×·¥ÀÇ °æ¿ì Ãß°¡ ¿À¹ö Çìµå°¡ ÇÊ¿äÇÏÁö ¾Ê½À´Ï´Ù. µ¿½Ã 󸮰¡ ¸¹Àº ¾ÛÀÇ °æ¿ì ÆíÁýÁõ µ¿±âÈ·Î ±â¾Æ »óÅÂ, ±³Âø »óÅ ¶Ç´Â ºÒÇÊ¿äÇÑ °¡ºñÁö ¼öÁý ÀϽà ÁßÁö°¡ ¹ß»ýÇÒ ¼ö ÀÖ½À´Ï´Ù. Tim Howland¿Í ¸¶Âù°¡Áö·Î ConcurrentHashMap
´ë½Å »ç¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù.
Hashtable:
Hashtable is a data structure that retains values of key-value pair. It doesn't allow null for both the keys and the values. You will get a NullPointerException
if you add null value. It is synchronized. So it comes with its cost. Only one thread can access HashTable at a particular time.
¿¹ :
import java.util.Map;
import java.util.Hashtable;
public class TestClass {
public static void main(String args[ ]) {
Map<Integer,String> states= new Hashtable<Integer,String>();
states.put(1, "INDIA");
states.put(2, "USA");
states.put(3, null); //will throw NullPointerEcxeption at runtime
System.out.println(states.get(1));
System.out.println(states.get(2));
// System.out.println(states.get(3));
}
}
HashMap:
HashMap is like Hashtable but it also accepts key value pair. It allows null for both the keys and the values. Its performance better is better than HashTable
, because it is unsynchronized
.
¿¹:
import java.util.HashMap;
import java.util.Map;
public class TestClass {
public static void main(String args[ ]) {
Map<Integer,String> states = new HashMap<Integer,String>();
states.put(1, "INDIA");
states.put(2, "USA");
states.put(3, null); // Okay
states.put(null,"UK");
System.out.println(states.get(1));
System.out.println(states.get(2));
System.out.println(states.get(3));
}
}