{"id":125413,"date":"2020-06-19T21:43:31","date_gmt":"2020-06-19T13:43:31","guid":{"rendered":"http:\/\/4563.org\/?p=125413"},"modified":"2020-06-19T21:43:31","modified_gmt":"2020-06-19T13:43:31","slug":"%e5%a4%9a%e7%ba%bf%e7%a8%8b%e6%93%8d%e4%bd%9c%e9%9d%9e%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8-map-%e5%ae%b9%e5%99%a8%ef%bc%8c%e8%af%bb%e5%86%99%e9%87%8c%e9%9d%a2%e7%9a%84%e5%85%83%e7%b4%a0%e6%98%af","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=125413","title":{"rendered":"\u591a\u7ebf\u7a0b\u64cd\u4f5c\u975e\u7ebf\u7a0b\u5b89\u5168 map \u5bb9\u5668\uff0c\u8bfb\u5199\u91cc\u9762\u7684\u5143\u7d20\u662f\u5426\u4e5f\u9700\u8981\u52a0\u9501\u8fdb\u884c\u64cd\u4f5c\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u591a\u7ebf\u7a0b\u64cd\u4f5c\u975e\u7ebf\u7a0b\u5b89\u5168 map \u5bb9\u5668\uff0c\u8bfb\u5199\u91cc\u9762\u7684\u5143\u7d20\u662f\u5426\u4e5f\u9700\u8981\u52a0\u9501\u8fdb\u884c\u64cd\u4f5c\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : SmaliYu <\/span>  <span><i><\/i> 5<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6211\u4e00\u76f4\u6709\u8fd9\u6837\u7684\u7591\u95ee\uff0c\u6bd4\u5982\u6709\u4e00\u4e2a Map&lt;String, Student&gt; map \uff1b\u51e0\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5bf9\u8fd9\u4e2a map \u8fdb\u884c\u6dfb\u52a0\uff0c\u5220\u9664\uff0c\u4fee\u6539\u91cc\u9762 Student \u91cc\u7684\u5c5e\u6027\u503c\uff0c\u662f\u4e0d\u662f\u8fd9\u4e09\u79cd\u64cd\u4f5c\u90fd\u9700\u8981\u9501\u4f4f\uff1f \u6211\u5f88\u660e\u786e\u64cd\u4f5c\u5bf9\u8fd9\u4e2a map \u8fdb\u884c\u6dfb\u52a0\uff0c\u5220\u9664\uff0c\u83b7\u53d6 size \u90fd\u662f\u9700\u8981\u9501\u4f4f\u7684\uff0c\u90a3\u4e48\u4fee\u6539\u91cc\u9762\u5143\u7d20\u662f\u4e0d\u662f\u4e5f\u9700\u8981\u5462\uff1f \u8fd9\u4e2a\u95ee\u9898\u4e3b\u8981\u9488\u5bf9 C++\u548c Java \u3002<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>17<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2144215\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nightwitch <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53ea\u8bf4 C++<br \/>\u4fee\u6539\u5143\u7d20-&gt;\u52a0\u9501\uff0c\u4e0d\u52a0\u7684\u8bdd\u5982\u679c\u4f60\u6709\u4e24\u4e2a\u7ebf\u7a0b\u540c\u65f6\u4fee\u6539\u5230\u4e86\u4e00\u4e2a student \u90a3\u5c31\u50bb\u903c\u4e86\u3002 <br \/>\u589e\u52a0\u5143\u7d20-&gt;\u5e94\u8be5\u53ef\u4ee5\u4e0d\u7528\uff0cmap \u4fdd\u8bc1 insert \u4e0d\u4f1a\u4f7f\u5176\u4ed6\u8fed\u4ee3\u5668\u5931\u6548<br \/>\u5220\u9664\u5143\u7d20-&gt;\u52a0\uff0c\u867d\u7136 map \u4fdd\u8bc1\u53ea\u5f71\u54cd\u88ab\u5220\u9664\u7684\u5143\u7d20\uff0c\u4f46\u662f\u5982\u679c\u53e6\u4e00\u4e2a\u7ebf\u7a0b\u5728\u64cd\u4f5c\u88ab\u5220\u9664\u7684\u5143\u7d20\u5c31\u4f1a\u78b0\u89c1\u95ee\u9898\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2144216\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xiangyuecn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u65e2\u7136\u975e\u7ebf\u7a0b\u5b89\u5168\uff0c\u8bfb\u5199\u90fd\u52a0\u9501\u5b8c\u4e8b\u3002\u636e\u6211\u6240\u77e5\uff1ac++\u4e0d\u77e5\u9053\uff0cjava \u3001c#\u7684\u975e\u7ebf\u7a0b\u5b89\u5168 map \u5185\u90e8\u5b58\u5728 while(true)\u7c7b\u4f3c\u5b9e\u73b0\uff0c\u5e76\u53d1\u64cd\u4f5c\u5b58\u5728\u4e00\u5b9a\u6982\u7387\u4f1a\u9020\u6210\u6b7b\u5faa\u73af\uff0c\u9020\u6210\u7a0b\u5e8f\u5047\u6b7b \u5185\u5b58\u66b4\u6da8\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2144217\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xuanbg <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u770b\u4f60\u8bfb\u4e4b\u540e\u662f\u4e0d\u662f\u8fd8\u8981\u5199\uff0c\u8981\u5199\u5c31\u8981\u9501\uff0c\u53ea\u8bfb\u4e0d\u9700\u8981\u9501\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2144218\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ccpp132 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8bfb\u5199\u90fd\u8981\uff0c\u6240\u6709\u64cd\u4f5c\u90fd\u8981\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2144219\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : shuax <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u52a0\u8bfb\u5199\u9501                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2144220\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : SmaliYu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @nightwitch \u8c22\u8c22\u60a8\uff0c\u89e3\u51b3\u4e86\u6211\u591a\u5e74\u4ee5\u6765\u7684\u7591\u60d1\uff0c\u518d\u6b21\u8c22\u8c22\u60a8\u2026\u2026                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2144221\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : killmojo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u53ef\u4ee5\u53c2\u8003\u6570\u636e\u5e93\u7684\u56db\u4e2a\u9694\u79bb\u7ea7\u522b\uff0c\u4ece\u9700\u6c42\u51fa\u53d1\u786e\u5b9a\u4ec0\u4e48\u60c5\u51b5\u8981\u52a0\u9501\uff0c\u6bd4\u5982\u51fa\u73b0\u6570\u636e\u53ea\u662f\u6d4f\u89c8\uff0c\u51fa\u73b0\u5e7b\u8c61\u8bfb\u65e0\u6240\u8c13\uff0c\u90a3\u5c31\u4e0d\u5fc5\u8bfb\u4e5f\u52a0\u9501\u3002<br \/>\u73b0\u5728\u5f88\u591a\u505a\u7535\u5b50\u8868\u683c\u6587\u6863\u534f\u540c\u7684\u4e5f\u662f\u7c7b\u4f3c\u95ee\u9898\uff0c\u4ece\u9700\u6c42\u89d2\u5ea6\u51fa\u53d1\u505a\u597d\u9700\u6c42\u7684\u9650\u5236\uff0c\u6280\u672f\u4e0a\u5b9e\u73b0\u5c31\u7b80\u5355\u4e00\u4e9b\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2144222\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lxk11153 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u611f\u89c9\u4f60\u6ca1\u63cf\u8ff0\u6e05\u695a\uff0c\u662f\u5982\u4e0b\u5417\uff1f<br \/>\u6761\u4ef6 1. \u975e\u7ebf\u7a0b\u5b89\u5168 map 2. \u6211\u53c2\u8003 java hashMap&lt;String, Student&gt;\u7684 3. \u9501\u7684\u9897\u7c92\u662f\u9488\u5bf9 map \u8fd9\u4e2a\u5b9e\u4f8b<br \/>1. map.put &#8211; \u52a0\u9501 https:\/\/juejin.im\/post\/5a66a08d5188253dc3321da0<br \/>2. map.remove &#8211; \u4e0d\u6e05\u695a\uff0c\u5e94\u8be5\u8981\u5427\uff1f<br \/>3. map.get &#8211; \u9700\u8981\u5417\uff1f<br \/>4. map.size &#8211; \u9700\u8981\u5417\uff1f see java.util.HashMap#size()<br \/>&#8212;- \u4f46\u4f60\u770b java.util.Collections#synchronizedMap(Map&lt;K, V&gt;) \u770b\u4f3c\u628a\u6240\u6709\u65b9\u6cd5\u90fd\u52a0\u9501\u4e86\uff0c\u4fdd\u8bc1\u4e0d\u51fa\u9519\u5427\u6216\u8005\u5b83\u8fd9\u4e2a synchronized \u5c31\u662f\u6307\u540c\u6b65\u6240\u6709<br \/>5. student.name=&#8221;new&#8221; &#8211; \u4e0d\u9700\u8981<\/p>\n<p>ps: \u6211\u7a81\u7136\u60f3\u5230 hashCode \u7684\u95ee\u9898, \u5047\u8bbe Student \u4f7f\u7528\u4e86 https:\/\/projectlombok.org\/features\/EqualsAndHashCode<br \/>\uff0cmap=hashMap&lt;Student, Object&gt;<br \/>Student s1=new Student(&#8220;name&#8221;);<br \/>map.put(s1, &#8220;name&#8221;);<br \/>s1.name=&#8221;new&#8221;; \/\/ \u4f1a\u5f15\u8d77 hashCode \u53d8\u5316\u5427\uff1f<br \/>map.put(s1, &#8220;new&#8221;); \/\/ map.size \u53d8\u6210 2 \u4e86\u5427\uff0c\u54c8\u54c8\u54c8                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2144223\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : BQsummer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u751f\u4ea7\u6559\u8bad\uff1amap \u7684 put \u8981\u52a0\u9501\uff0c\u6269\u5bb9\u65f6\u5bfc\u81f4\u6570\u636e\u4e22\u5931\uff08 java \uff09                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2144224\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wysnylc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @BQsummer #8 \u7528\u7684 HashMap \u800c\u4e0d\u662f ConcurrentHashMap?                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2144225\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lniwn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @nightwitch #1 \u7ea0\u6b63\u4e00\u70b9\uff0c\u867d\u7136 add \u64cd\u4f5c\u4e0d\u4f1a\u4f7f\u8fed\u4ee3\u5668\u5931\u6548\uff0c\u4f46\u662f\u4f1a\u4fee\u6539\u8282\u70b9\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u6d89\u53ca\u5230\u7ea2\u9ed1\u6811\u7684\u81ea\u5e73\u8861\uff0c\u6240\u4ee5\u4efb\u4f55 update \u64cd\u4f5c\u90fd\u9700\u8981\u52a0\u9501\u3002\u53ef\u4ee5\u4f7f\u7528\u8bfb\u5199\u9501\uff0cread \u64cd\u4f5c\u52a0\u8bfb\u9501\uff0cupdate \u64cd\u4f5c\u52a0\u5199\u9501\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2144226\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mind3x <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @SmaliYu \u4ed6\u8bf4\u7684\u662f\u9519\u7684\uff0cinsert \u4e0d\u4f7f\u5176\u4ed6\u8fed\u4ee3\u5668\u5931\u6548\u672c\u6765\u524d\u63d0\u5c31\u662f\u5355\u7ebf\u7a0b\u3002\u591a\u7ebf\u7a0b\u8bfb\u5199\u5b8c\u5168\u4e0d\u662f\u4e00\u56de\u4e8b                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2144227\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : CRVV <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8fd9\u79cd\u4e8b\u60c5\u8981\u770b\u6587\u6863\u7684<\/p>\n<p>\u5728 C++ \u91cc\uff0c\u5bb9\u5668\u4e0a\u7684 const member function \u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<br \/>\u7136\u540e\u5728\u8fd9\u91cc\u9762 https:\/\/en.cppreference.com\/w\/cpp\/container\/map<\/p>\n<p>const T&amp; at( const Key&amp; key ) const; \u662f\u7ebf\u7a0b\u5b89\u5168\u7684<br \/>T&amp; operator[]( Key&amp;&amp; key ); \u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u6240\u4ee5\u4ece specification \u7684\u89d2\u5ea6\u6765\u8bf4\uff0c\u6709\u4e24\u4e2a\u7ebf\u7a0b\u90fd\u5728 m[key] \u4e5f\u662f\u9519\u7684\u3002<br \/>insert \u5f53\u7136\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\uff0c\u660e\u663e\u4e0d\u53ef\u80fd<\/p>\n<p>\u5fc5\u987b\u6240\u6709\u7ebf\u7a0b\u4e0a\u90fd\u53ea\u4f7f\u7528 const member function \u624d\u6ca1\u6709 data race\uff0c\u6240\u4ee5\u5b9e\u9645\u60c5\u51b5\u901a\u5e38\u662f\u8fd9\u4e9b\u64cd\u4f5c\u5168\u90fd\u8981\u52a0\u9501\u3002<\/p>\n<p>Java \u7684 HashMap\uff0chttps:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/util\/HashMap.html<\/p>\n<p>If multiple threads access a hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.)<\/p>\n<p>\u6dfb\u52a0\u5220\u9664\u90fd\u9700\u8981\u52a0\u9501\uff0c\u4fee\u6539\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684 entry \u4e0d\u9700\u8981\u3002                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2144228\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : BQsummer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wysnylc HashMap, \u5bf9\u8c61\u4e22\u5230\u7ebf\u7a0b\u6c60\u7684\u591a\u4e2a task \u91cc\u4e86\uff0c\u8001\u4ee3\u7801\u80cc\u4e86\u4e2a 4 \u7ea7\u4e8b\u6545                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2144229\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wysnylc <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @BQsummer #13 java8 \u4e4b\u524d\u7684 HashMap \u6269\u5bb9\u4f1a\u5bfc\u81f4\u6b7b\u5faa\u73af(\u5934\u63d2\u6cd5),java8 \u4e4b\u540e\u624d\u662f\u4e22\u6570\u636e(\u5c3e\u63d2\u6cd5)<br \/>\u89e3\u51b3 map \u5e76\u53d1\u65b9\u6848\u7b80\u5355\u7c97\u66b4,\u6539\u6210 ConcurrentHashMap \u6216\u8005 ConcurrentSkipListMap(\u63a8\u8350)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2144230\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Wirbelwind <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             c u d \u90fd\u8981\u52a0\u9501\uff0c\u4e0d\u7136\u4f1a\u53d1\u751f\u5404\u79cd\u4f60\u80fd\u60f3\u5230\u548c\u60f3\u4e0d\u5230\u7684\u60c5\u51b5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2144231\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : snnn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             No.                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u591a\u7ebf\u7a0b\u64cd\u4f5c\u975e\u7ebf\u7a0b\u5b89\u5168 map \u5bb9\u5668&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/125413"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=125413"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/125413\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=125413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=125413"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=125413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}