{"id":2071,"date":"2017-07-09T12:06:53","date_gmt":"2017-07-09T10:06:53","guid":{"rendered":"http:\/\/www.itersdesktop.com\/?p=2071"},"modified":"2017-07-09T23:47:47","modified_gmt":"2017-07-09T21:47:47","slug":"hashmap-class-in-java","status":"publish","type":"post","link":"https:\/\/www.itersdesktop.com\/vi\/2017\/07\/09\/hashmap-class-in-java\/","title":{"rendered":"L\u1edbp HashMap trong Java"},"content":{"rendered":"<p><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_86 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a7b3bb2b5419\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a7b3bb2b5419\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.itersdesktop.com\/vi\/2017\/07\/09\/hashmap-class-in-java\/#gioi-thieu\" >Gi\u1edbi thi\u1ec7u<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.itersdesktop.com\/vi\/2017\/07\/09\/hashmap-class-in-java\/#cach-haspmap-hoat-dong\" >C\u00e1ch HaspMap ho\u1ea1t \u0111\u1ed9ng<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.itersdesktop.com\/vi\/2017\/07\/09\/hashmap-class-in-java\/#ham-bam-gia-tri-bam-va-thung-chua-la-gi\" >H\u00e0m b\u0103m, gi\u00e1 tr\u1ecb b\u0103m v\u00e0 th\u00f9ng ch\u1ee9a l\u00e0 g\u00ec?<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.itersdesktop.com\/vi\/2017\/07\/09\/hashmap-class-in-java\/#tham-khao-them\" >Tham kh\u1ea3o th\u00eam:<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"gioi-thieu\"><\/span>Gi\u1edbi thi\u1ec7u<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><strong><span style=\"font-family: 'andale mono', monospace;\">HashMap<\/span><\/strong> l\u00e0 l\u1edbp s\u1eed d\u1ee5ng m\u1ed9t b\u1ea3ng b\u0103m \u0111\u1ec3 c\u00e0i \u0111\u1eb7t m\u1ed9t ki\u1ec3u d\u1eef li\u1ec7u t\u1eeb\u00a0giao di\u1ec7n <span style=\"font-family: 'andale mono', monospace;\">Map<\/span> (Map interface). N\u00f3 gi\u1ea3m th\u1eddi gian th\u1ef1c hi\u1ec7n c\u00e1c ph\u00e9p to\u00e1n c\u01a1 b\u1ea3n, nh\u01b0 <span style=\"font-family: 'andale mono', monospace;\">get()<\/span> v\u00e0 <span style=\"font-family: 'andale mono', monospace;\">put()<\/span>, xu\u1ed1ng c\u00f2n h\u1eb1ng s\u1ed1 th\u1eadm ch\u00ed khi l\u00e0m vi\u1ec7c nh\u1eefng t\u1eadp d\u1eef li\u1ec7u l\u1edbn.<\/p>\n<figure id=\"attachment_2676\" aria-describedby=\"caption-attachment-2676\" style=\"width: 593px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2676\" src=\"http:\/\/www.itersdesktop.com\/wp-content\/uploads\/2016\/12\/Collection_MapImplementation.png\" alt=\"The Map Hierarchy\" width=\"593\" height=\"292\" srcset=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2016\/12\/Collection_MapImplementation.png 593w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2016\/12\/Collection_MapImplementation-300x148.png 300w\" sizes=\"auto, (max-width: 593px) 100vw, 593px\" \/><figcaption id=\"caption-attachment-2676\" class=\"wp-caption-text\">The Map Hierarchy<\/figcaption><\/figure>\n<h2><span class=\"ez-toc-section\" id=\"cach-haspmap-hoat-dong\"><\/span>C\u00e1ch HaspMap ho\u1ea1t \u0111\u1ed9ng<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>HashMap ho\u1ea1t \u0111\u1ed9ng d\u1ef1a tr\u00ean nguy\u00ean l\u00ed c\u1ee7a thu\u1eadt to\u00e1n b\u0103m (hashing). \u0110\u1ec3 hi\u1ec3u c\u00e1ch th\u1ee9c b\u0103m, ch\u00fang ta n\u00ean hi\u1ec3u ba thu\u1eadt ng\u1eef \u0111\u1ea7u ti\u00ean, \u0111\u00f3 l\u00e0 H\u00e0m b\u0103m (Hash Function), Gi\u00e1 tr\u1ecb b\u0103m (Hash Value), v\u00e0 Th\u00f9ng\/R\u1ed5 ch\u1ee9a c\u00e1c gi\u00e1 tr\u1ecb b\u0103m\u00a0(Bucket).<\/p>\n<h3><span class=\"ez-toc-section\" id=\"ham-bam-gia-tri-bam-va-thung-chua-la-gi\"><\/span>H\u00e0m b\u0103m, gi\u00e1 tr\u1ecb b\u0103m v\u00e0 th\u00f9ng ch\u1ee9a l\u00e0 g\u00ec?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>H\u00e0m hashCode() tr\u1ea3 v\u1ec1 m\u1ed9t gi\u00e1 tr\u1ecb nguy\u00ean l\u00e0m vai tr\u00f2 c\u1ee7a m\u1ed9t h\u00e0m b\u0103m. Quan tr\u1ecdng, c\u1ea7n l\u01b0u \u00fd r\u1eb1ng ph\u01b0\u01a1ng th\u1ee9c n\u00e0y xu\u1ea5t hi\u1ec7n trong l\u1edbp Object (l\u00e0 l\u1edbp cha\/m\u1eb9 c\u1ee7a m\u1ecdi l\u1edbp kh\u00e1c trong Java).<\/p>\n<p>\u0110\u00e2y l\u00e0 m\u00f4 t\u1ea3 h\u00e0m b\u0103m (hash function) (\u0111\u01b0\u1ee3c \u00a0bi\u1ebft nh\u01b0 l\u00e0 ph\u01b0\u01a1ng th\u1ee9c hashCode) trong l\u1edbp Object:<\/p>\n<pre class=\"lang:java decode:true\">public native int hashCode()\r\n<\/pre>\n<p>\u0110i\u1ec3m quan tr\u1ecdng c\u1ea7n l\u01b0u \u00fd \u1edf \u0111\u00e2y l\u00e0 ph\u01b0\u01a1ng th\u1ee9c hashCode \u1edf tr\u00ean tr\u1ea3 v\u1ec1 m\u1ed9t gi\u00e1 tr\u1ecb ki\u1ec3u s\u1ed1 nguy\u00ean. V\u00ec v\u1eady gi\u00e1 tr\u1ecb b\u0103m (hash value) l\u00e0 gi\u00e1 tr\u1ecb nguy\u00ean \u0111\u01b0\u1ee3c h\u00e0m b\u0103m \u1edf tr\u00ean tr\u1ea3 v\u1ec1.<\/p>\n<figure id=\"attachment_2094\" aria-describedby=\"caption-attachment-2094\" style=\"width: 500px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/www.itersdesktop.com\/wp-content\/uploads\/2015\/07\/how-hashmap-works-internally-in-java-.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-2094\" src=\"http:\/\/www.itersdesktop.com\/wp-content\/uploads\/2015\/07\/how-hashmap-works-internally-in-java-.png\" alt=\"how hashmap works internally in java\" width=\"500\" height=\"344\" srcset=\"https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2015\/07\/how-hashmap-works-internally-in-java-.png 500w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2015\/07\/how-hashmap-works-internally-in-java--300x206.png 300w, https:\/\/www.itersdesktop.com\/wp-content\/uploads\/2015\/07\/how-hashmap-works-internally-in-java--50x34.png 50w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><figcaption id=\"caption-attachment-2094\" class=\"wp-caption-text\">How HashMap works internally in java<\/figcaption><\/figure>\n<p>L\u1edbp HashMap h\u1ed7 tr\u1ee3 4 ph\u01b0\u01a1ng th\u1ee9c t\u1ea1o\/kh\u1edfi t\u1ea1o \u0111\u1ed1i t\u01b0\u1ee3ng. Ph\u01b0\u01a1ng th\u1ee9c \u0111\u1ea7u ti\u00ean \u0111\u1ec3 t\u1ea1o\/kh\u1edfi t\u1ea1o m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng HashMap l\u00e0 x\u00e2y d\u1ef1ng m\u1ed9t b\u1ea3ng b\u0103m ng\u1ea7m \u0111\u1ecbnh:<\/p>\n<pre class=\"lang:java decode:true \">HashMap()<\/pre>\n<p>Ph\u01b0\u01a1ng th\u1ee9c th\u1ee9 hai kh\u1edfi t\u1ea1o m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng HashMap b\u1eb1ng vi\u1ec7c s\u1eed d\u1ee5ng m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng HashMap kh\u00e1c, c\u00f2n \u0111\u01b0\u1ee3c g\u1ecdi l\u00e0 h\u00e0m t\u1ea1o sao ch\u00e9p.<\/p>\n<pre class=\"lang:java decode:true\">HashMap(Map m)\r\n<\/pre>\n<p>Ph\u01b0\u01a1ng th\u1ee9c th\u1ee9 ba kh\u1edfi t\u1ea1o m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng b\u1ea3ng b\u0103m v\u1edbi s\u1ee9c ch\u1ee9a (capacity) c\u1ee5 th\u1ec3.<\/p>\n<pre class=\"lang:java decode:true \">HashMap(int capacity)<\/pre>\n<p>Ph\u01b0\u01a1ng th\u1ee9c th\u1ee9 t\u01b0 kh\u1edfi t\u1ea1o m\u1ed9t \u0111\u1ed1i t\u01b0\u1ee3ng b\u1ea3ng b\u0103m v\u1edbi hai tham s\u1ed1 s\u1ee9c ch\u1ee9a (capacity) v\u00e0 t\u1ec9 l\u1ec7 l\u00e0m \u0111\u1ea7y b\u1ea3ng b\u0103m (fillRatio).<\/p>\n<pre class=\"lang:java decode:true\">HashMap(int capacity, float fillRatio)<\/pre>\n<p>M\u1ed9t s\u1ed1 ph\u01b0\u01a1ng th\u1ee9c \u0111\u01b0\u1ee3c \u0111\u1ecbnh ngh\u0129a th\u00eam trong l\u1edbp HashMap<\/p>\n<p>Ngo\u00e0i nh\u1eefng ph\u01b0\u01a1ng th\u1ee9c k\u1ebf th\u1eeba t\u1eeb c\u00e1c l\u1edbp cha, l\u1edbp HashMap \u0111\u1ecbnh ngh\u0129a th\u00eam nh\u1eefng ph\u01b0\u01a1ng th\u1ee9c sau:<\/p>\n<p>VD: V\u00ed d\u1ee5 sau \u0111\u00e2y minh h\u1ecda m\u1ed9t v\u00e0i ph\u01b0\u01a1ng th\u1ee9c m\u00e0 l\u1edbp n\u00e0y h\u1ed7 tr\u1ee3.<\/p>\n<pre class=\"theme:github font:monospace decode-attributes:true lang:java decode:true \">import java.util.*;\r\n\r\npublic class HashMapDemo {\r\n\r\n   public static void main(String args[]) {\r\n   \r\n      \/\/ Create a hash map\r\n      HashMap hm = new HashMap();\r\n      \/\/ Put elements to the map\r\n      hm.put(\"Zara\", new Double(3434.34));\r\n      hm.put(\"Mahnaz\", new Double(123.22));\r\n      hm.put(\"Ayan\", new Double(1378.00));\r\n      hm.put(\"Daisy\", new Double(99.22));\r\n      hm.put(\"Qadir\", new Double(-19.08));\r\n      \r\n      \/\/ Get a set of the entries\r\n      Set set = hm.entrySet();\r\n      \/\/ Get an iterator\r\n      Iterator i = set.iterator();\r\n      \/\/ Display elements\r\n      while(i.hasNext()) {\r\n         Map.Entry me = (Map.Entry)i.next();\r\n         System.out.print(me.getKey() + \": \");\r\n         System.out.println(me.getValue());\r\n      }\r\n      System.out.println();\r\n      \/\/ Deposit 1000 into Zara's account\r\n      double balance = ((Double)hm.get(\"Zara\")).doubleValue();\r\n      hm.put(\"Zara\", new Double(balance + 1000));\r\n      System.out.println(\"Zara's new balance: \" +\r\n      hm.get(\"Zara\"));\r\n   }\r\n}<\/pre>\n<p>Ch\u01b0\u01a1ng tr\u00ecnh n\u00e0y s\u1ebd sinh ra k\u1ebft qu\u1ea3 nh\u01b0 sau:<\/p>\n<pre class=\"lang:java decode:true\">Zara: 3434.34\r\nMahnaz: 123.22\r\nDaisy: 99.22\r\nAyan: 1378.0\r\nQadir: -19.08\r\n\r\nZara's new balance: 4434.34<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"tham-khao-them\"><\/span>Tham kh\u1ea3o th\u00eam:<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/HashMap.html<\/p>\n<p>http:\/\/examples.javacodegeeks.com\/core-java\/util\/hashmap\/hashmap-changes-in-java-8\/<\/p>\n<p>http:\/\/javahungry.blogspot.com\/2013\/08\/hashing-how-hash-map-works-in-java-or.html<\/p>\n<p>http:\/\/sinhvienit.net\/forum\/hashmap-trong-java.353561.html<\/p>\n<p>http:\/\/www.tutorialspoint.com\/java\/java_hashmap_class.htm<\/p>","protected":false},"excerpt":{"rendered":"<p>Gi\u1edbi thi\u1ec7u HashMap l\u00e0 l\u1edbp s\u1eed d\u1ee5ng m\u1ed9t b\u1ea3ng b\u0103m \u0111\u1ec3 c\u00e0i \u0111\u1eb7t m\u1ed9t ki\u1ec3u d\u1eef li\u1ec7u t\u1eeb\u00a0giao di\u1ec7n Map (Map interface). N\u00f3 gi\u1ea3m th\u1eddi gian th\u1ef1c hi\u1ec7n c\u00e1c ph\u00e9p&hellip; <\/p>\n","protected":false},"author":2,"featured_media":2675,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,10],"tags":[624,625],"class_list":["post-2071","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-java-techs","tag-hashmap","tag-map"],"_links":{"self":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/2071","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/comments?post=2071"}],"version-history":[{"count":29,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/2071\/revisions"}],"predecessor-version":[{"id":2679,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/posts\/2071\/revisions\/2679"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/media\/2675"}],"wp:attachment":[{"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/media?parent=2071"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/categories?post=2071"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.itersdesktop.com\/vi\/wp-json\/wp\/v2\/tags?post=2071"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}