{"id":5259,"date":"2021-08-09T18:29:42","date_gmt":"2021-08-09T10:29:42","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=5259"},"modified":"2021-08-09T18:29:42","modified_gmt":"2021-08-09T10:29:42","slug":"%e5%a6%82%e4%bd%95%e4%bf%9d%e8%af%81linkedhashmap%e4%bb%a5%e5%8f%8a%e5%ae%83%e5%ae%9e%e7%8e%b0lru%e7%bc%93%e5%ad%98%e7%ba%bf%e7%a8%8b%e5%ae%89%e5%85%a8","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=5259","title":{"rendered":"\u5982\u4f55\u4fdd\u8bc1LinkedHashMap\u4ee5\u53ca\u5b83\u5b9e\u73b0LRU\u7f13\u5b58\u7ebf\u7a0b\u5b89\u5168"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u672c\u6587\u9996\u53d1\u4e8e\u6211\u7684\u4e2a\u4eba\u516c\u4f17\u53f7\u201c\u4e1a\u4f59\u8349\u201d\uff0c\u7531\u4e8e\u516c\u4f17\u53f7\u4e0d\u5229\u4e8e\u641c\u7d22\uff0c\u8fd9\u91cc\u540c\u6b65\u5230\u6211\u7684\u4e2a\u4eba\u7f51\u7ad9\u4e0a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6628\u5929\u6709\u4e00\u4f4d\u7f51\u53cb\u5bf9\u6211\u7684\u4e00\u7bc7\u6587\u7ae0\u8fdb\u884c\u4e86\u8bc4\u8bba\uff0c\u7559\u8a00\u8bf4\u5230\uff1a<code>\u7ebf\u7a0b\u5b89\u5168\u7684 LinkedHashMap \u600e\u4e48\u5b9e\u73b0\uff1f<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2021\/08\/lhm0.jpg\" alt=\"\u7ebf\u7a0b\u5b89\u5168\u7684 LinkedHashMap\" \/><figcaption>\u7ebf\u7a0b\u5b89\u5168\u7684 LinkedHashMap<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u6211\u8fd9\u91cc\u82b1\u51e0\u5206\u949f\u65f6\u95f4\u804a\u4e00\u4e0b\u8fd9\u4e2a\u95ee\u9898\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u9605\u8bfb\u6211\u524d\u9762\u7684\u6587\u7ae0\uff0c\u6211\u4eec\u77e5\u9053\uff1a<code>HashMap \u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u5c31\u662f\u8fed\u4ee3 HashMap \u7684\u987a\u5e8f\u5e76\u4e0d\u662f HashMap \u653e\u7f6e\u7684\u987a\u5e8f\uff0c\u4e5f\u5c31\u662f\u65e0\u5e8f\u3002HashMap \u7684\u8fd9\u4e00\u7f3a\u70b9\u5f80\u5f80\u4f1a\u5e26\u6765\u56f0\u6270\uff0c\u56e0\u4e3a\u6709\u4e9b\u573a\u666f\uff0c\u6211\u4eec\u671f\u5f85\u4e00\u4e2a\u6709\u5e8f\u7684 Map<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u65f6\u5019\uff0cLinkedHashMap \u5c31\u95ea\u4eae\u767b\u573a\u4e86\uff0c\u5b83\u867d\u7136\u589e\u52a0\u4e86\u65f6\u95f4\u548c\u7a7a\u95f4\u4e0a\u7684\u5f00\u9500\uff0c\u4f46\u662f\u901a\u8fc7\u7ef4\u62a4\u4e00\u4e2a\u8fd0\u884c\u4e8e\u6240\u6709\u6761\u76ee\u7684\u53cc\u5411\u94fe\u8868\uff0cLinkedHashMap \u4fdd\u8bc1\u4e86\u5143\u7d20\u8fed\u4ee3\u7684\u987a\u5e8f\u3002\u8be5\u8fed\u4ee3\u987a\u5e8f\u53ef\u4ee5\u662f\u63d2\u5165\u987a\u5e8f\u6216\u8005\u662f\u8bbf\u95ee\u987a\u5e8f\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2021\/08\/lhm1.jpg\" alt=\"LinkedHashMap\u9762\u8bd5\u70b9\" \/><figcaption>LinkedHashMap\u9762\u8bd5\u70b9<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">LinkedHashMap \u53ef\u4ee5\u8ba4\u4e3a\u662f HashMap + LinkedList\uff0c\u5373\u5b83\u65e2\u4f7f\u7528 HashMap \u64cd\u4f5c\u6570\u636e\u7ed3\u6784\uff0c\u53c8\u4f7f\u7528 LinkedList \u7ef4\u62a4\u63d2\u5165\u5143\u7d20\u7684\u5148\u540e\u987a\u5e8f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0a\u9762\u5e2e\u52a9\u5927\u5bb6\u7b80\u5355\u56de\u987e\u4e86\u4e00\u4e0b LinkedHashMap \u540e\uff0c\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u518d\u6765\u770b\u770b\u5982\u4f55\u628a LinkedHashMap \u53d8\u5f97\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u4f73\u7b54\u6848\u5c31\u662f\u6211\u4eec\u53ef\u4ee5\u533f\u540d\u6269\u5c55 LinkedHashMap \u6765\u66f4\u6539 removeEldestEntry(&#8230;) \u7684\u884c\u4e3a\uff0c\u7136\u540e\u5c06\u533f\u540d\u7c7b\u7684\u5b9e\u4f8b\u5305\u88c5\u5728\u540c\u6b65\u6620\u5c04\u4e2d\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Map&lt;String, Integer&gt; map = Collections.synchronizedMap(new LinkedHashMap&lt;String, Integer&gt;() {\n   private static final long serialVersionUID = 1L;\n   @Override\n   protected boolean removeEldestEntry(Entry&lt;String, Integer&gt; eldest) {\n      return size() &gt; MAX_SIZE;\n   }               \n});<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5373\u5229\u7528<code>Collections.synchronizedMap()<\/code>\u7684\u65b9\u6cd5\u5305\u88c5\u6210\u4e00\u4e2a thread-safe \u7684 Map\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>final class LruCache&lt;K, V&gt; extends LinkedHashMap&lt;K, V&gt; implements Cache&lt;K, V&gt; {\n    private static final long serialVersionUID = 1L;\n    private final int maxCapacity;\n \n    public LruCache(int maxCapacity) {\n        super(maxCapacity, 0.7F, true);\n        this.maxCapacity = maxCapacity;\n    }\n \n    public synchronized void addElement(K key, V value) {\n        this.put(key, value);\n    }\n \n    public synchronized V getElement(K key) {\n        return this.get(key);\n    }\n \n    public synchronized V removeElement(K key) {\n        return this.remove(key);\n    }\n \n    protected boolean removeEldestEntry(Entry&lt;K, V&gt; entry) {\n        return this.size() &gt; this.maxCapacity;\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679c\u4f60\u4e0d\u60f3\u4f7f\u7528 synchronized\uff0c\u4f7f\u7528 ReentrantLock \u4e5f\u53ef\u4ee5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u751a\u81f3\u662f\u53c2\u8003\u8457\u540d\u6846\u67b6 lucene \u4e2d\u7684<code>org.apache.lucene.util.collections.LRUHashMap<\/code>\uff0c\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u4e0d\u9519\u7684\u501f\u9274\uff01<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/xmt1139057136\/xttblog\/2021\/08\/lhm2.jpg\" alt=\"\u7ebf\u7a0b\u5b89\u5168\u7684 LRUHashMap\" \/><figcaption>\u7ebf\u7a0b\u5b89\u5168\u7684 LRUHashMap<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0a\u5185\u5bb9\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u5230\u5927\u5bb6\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u3002\u591a\u501f\u9274 ConcurrentLinkedQueue\u3001ConcurrentHashMap \u7b49\u5bb9\u5668\u7684\u5b9e\u73b0\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=5259\">\u5982\u4f55\u4fdd\u8bc1LinkedHashMap\u4ee5\u53ca\u5b83\u5b9e\u73b0LRU\u7f13\u5b58\u7ebf\u7a0b\u5b89\u5168<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u9996\u53d1\u4e8e\u6211\u7684\u4e2a\u4eba\u516c\u4f17\u53f7\u201c\u4e1a\u4f59\u8349\u201d\uff0c\u7531\u4e8e\u516c\u4f17\u53f7\u4e0d\u5229\u4e8e\u641c\u7d22\uff0c\u8fd9\u91cc\u540c\u6b65\u5230\u6211\u7684\u4e2a\u4eba\u7f51\u7ad9\u4e0a\u3002 \u6628\u5929\u6709\u4e00\u4f4d\u7f51\u53cb\u5bf9\u6211\u7684\u4e00\u7bc7 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1758,1957,1759,712,426],"class_list":["post-5259","post","type-post","status-publish","format-standard","hentry","category-java","tag-abstractqueuedsynchronizer","tag-lru","tag-nonreentrantlock","tag-reentrantlock","tag-synchronized"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5259","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5259"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5259\/revisions"}],"predecessor-version":[{"id":5260,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5259\/revisions\/5260"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}