{"id":81,"date":"2024-08-20T00:57:02","date_gmt":"2024-08-19T16:57:02","guid":{"rendered":"https:\/\/blog.easitch.com\/?p=81"},"modified":"2026-03-18T14:25:28","modified_gmt":"2026-03-18T06:25:28","slug":"%e8%ae%b0%e4%b8%80%e6%ac%a1%e6%9d%ad%e5%b7%9e%e6%96%bd%e5%bc%ba%e9%9b%86%e5%9b%a2%e9%9d%a2%e8%af%95%e9%a2%98","status":"publish","type":"post","link":"https:\/\/blog.eastfond.com\/?p=81","title":{"rendered":"\u8bb0\u4e00\u6b21\u676d\u5dde\u65bd\u5f3a\u96c6\u56e2\u9762\u8bd5\u9898"},"content":{"rendered":"<blockquote>\n<p>\u8fd8\u6709\u597d\u51e0\u4e2a\u5b9e\u5728\u60f3\u4e0d\u8d77\u6765\u4e86 \/(\u3112o\u3112)\/~~<\/p>\n<\/blockquote>\n<h1>openfeign \u7684\u6267\u884c\u539f\u7406<\/h1>\n<ol>\n<li>\u9996\u5148\u901a\u8fc7@EnableFeignClients\u6ce8\u89e3\u540e\u4f1a\u5bfc\u5165FeignClientsRegistrar\u8fd9\u4e2a\u7c7b\uff0c\u7c7b\u4e2d\u4f1a\u901a\u8fc7\u4e00\u4e2a\u65b9\u6cd5registerFeignClients\u626b\u63cf\u6240\u6709\u7684\u5b9a\u4e49\u4e86@FeignClients\u63a5\u53e3\u83b7\u53d6\u5230@FeignClients\u4e2d\u5b9a\u4e49\u7684\u5c5e\u6027\uff0c\u5e76\u4e14\u751f\u6210\u4ee3\u7406\u7c7b\u3002<\/li>\n<li>\u5728\u63a5\u53e3\u65b9\u6cd5\u4e0a\uff0cFeign \u4f1a\u901a\u8fc7SpringMvcContract\u7c7b\u4e2d\u7684processAnnotationOnClass\u65b9\u6cd5\u89e3\u6790\u5982 <code>@RequestMapping<\/code>\u3001<code>@PathVariable<\/code>\u3001<code>@RequestParam<\/code> \u7b49\u6ce8\u89e3\uff0c\u4ee5\u6784\u5efa\u5b9e\u9645\u7684 HTTP \u8bf7\u6c42\u3002<\/li>\n<li>\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u5982\u679c\u4ec0\u4e48\u90fd\u6ca1\u914d\u7f6e\u7684\u8bdd\u9ed8\u8ba4\u4f7f\u7528Apache HttpClient\uff0c\u4e5f\u53ef\u4ee5\u914d\u7f6e\u6210OKhttp\u3002<\/li>\n<li>\u54cd\u5e94\u5904\u7406 \uff1a\u63a5\u6536\u5230\u54cd\u5e94\u540e\uff0cFeign \u4f1a\u89e3\u6790\u8fd4\u56de\u7684\u6570\u636e\uff0c\u5e76\u6839\u636e\u65b9\u6cd5\u7684\u8fd4\u56de\u7c7b\u578b\u8fdb\u884c\u53cd\u5e8f\u5217\u5316\u3002\u5982\u679c\u8bf7\u6c42\u5931\u8d25\uff0cFeign \u4f1a\u6839\u636e\u914d\u7f6e\u7684\u9519\u8bef\u89e3\u7801\u5668\u5904\u7406\u5f02\u5e38\u60c5\u51b5\u3002<\/li>\n<\/ol>\n<h1>\u4f60\u4eecopenfeign\u7528\u7684\u957f\u94fe\u63a5\u8fd8\u662f\u77ed\u94fe\u63a5<\/h1>\n<p>openfeign\u9ed8\u8ba4\u4f7f\u7528\u7684\u77ed\u94fe\u63a5\u3002\u5373\u5f53\u8bf7\u6c42\u5b8c\u6bd5\u540e\u5c31\u5173\u95ed\u6b64\u6b21\u94fe\u63a5\uff0c\u4e0b\u6b21\u8bf7\u6c42\u518d\u5f00\u542f\u3002<\/p>\n<p>\u8fd9\u91cc\u4e5f\u8bf4\u4e00\u4e0b\u957f\u77ed\u94fe\u63a5\u7684\u4f18\u7f3a\u70b9<\/p>\n<p>\u4f7f\u7528\u957f\u8fde\u63a5\u548c\u77ed\u8fde\u63a5\u5404\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u7684\u573a\u666f\u4e5f\u6709\u6240\u4e0d\u540c\u3002<\/p>\n<h2>\u77ed\u8fde\u63a5<\/h2>\n<p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<ol>\n<li><strong>\u7b80\u5355\u6027<\/strong>\uff1a\u6bcf\u4e2a\u8bf7\u6c42\u540e\u81ea\u52a8\u5173\u95ed\u8fde\u63a5\uff0c\u51cf\u5c11\u4e86\u8fde\u63a5\u7ba1\u7406\u7684\u590d\u6742\u6027\u3002<\/li>\n<li><strong>\u8d44\u6e90\u91ca\u653e<\/strong>\uff1a\u6bcf\u6b21\u8bf7\u6c42\u540e\uff0c\u7cfb\u7edf\u4f1a\u91ca\u653e\u8d44\u6e90\uff0c\u51cf\u5c11\u6f5c\u5728\u7684\u8d44\u6e90\u6cc4\u9732\u98ce\u9669\u3002<\/li>\n<li><strong>\u9632\u6b62\u95f2\u7f6e<\/strong>\uff1a\u907f\u514d\u4e86\u957f\u65f6\u95f4\u95f2\u7f6e\u8fde\u63a5\u5e26\u6765\u7684\u8d44\u6e90\u6d6a\u8d39\u3002<\/li>\n<\/ol>\n<p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<ol>\n<li><strong>\u6027\u80fd\u635f\u5931<\/strong>\uff1a\u9891\u7e41\u7684\u5efa\u7acb\u548c\u5173\u95ed\u8fde\u63a5\u4f1a\u589e\u52a0\u5ef6\u8fdf\uff0c\u964d\u4f4e\u6027\u80fd\u3002<\/li>\n<li><strong>\u5f00\u9500<\/strong>\uff1a\u6bcf\u4e2a\u8bf7\u6c42\u90fd\u9700\u8981\u8fdb\u884c TCP \u63e1\u624b\uff0c\u589e\u52a0\u7f51\u7edc\u5f00\u9500\u3002<\/li>\n<\/ol>\n<p><strong>\u573a\u666f\u793a\u4f8b<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u4f4e\u9891\u8bf7\u6c42<\/strong>\uff1a\u4f8b\u5982\u4e00\u4e2a\u7528\u6237\u4fe1\u606f\u67e5\u8be2\u63a5\u53e3\uff0c\u7528\u6237\u5e76\u4e0d\u9891\u7e41\u8bbf\u95ee\uff0c\u4f7f\u7528\u77ed\u8fde\u63a5\u53ef\u4ee5\u51cf\u5c11\u8d44\u6e90\u5360\u7528\u3002<\/li>\n<\/ul>\n<h2>\u957f\u8fde\u63a5<\/h2>\n<p><strong>\u4f18\u70b9<\/strong>\uff1a<\/p>\n<ol>\n<li><strong>\u63d0\u9ad8\u6027\u80fd<\/strong>\uff1a\u8fde\u63a5\u53ef\u4ee5\u91cd\u7528\uff0c\u51cf\u5c11\u4e86 TCP \u63e1\u624b\u7684\u9891\u7387\uff0c\u964d\u4f4e\u5ef6\u8fdf\u3002<\/li>\n<li><strong>\u9002\u5408\u9ad8\u9891\u8bf7\u6c42<\/strong>\uff1a\u5728\u9ad8\u9891\u4ea4\u4e92\u573a\u666f\u4e2d\uff0c\u91cd\u7528\u8fde\u63a5\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7cfb\u7edf\u541e\u5410\u91cf\u3002<\/li>\n<\/ol>\n<p><strong>\u7f3a\u70b9<\/strong>\uff1a<\/p>\n<ol>\n<li><strong>\u8d44\u6e90\u5360\u7528<\/strong>\uff1a\u8fde\u63a5\u957f\u65f6\u95f4\u4fdd\u6301\u53ef\u80fd\u5bfc\u81f4\u8d44\u6e90\u6cc4\u9732\uff0c\u5c24\u5176\u662f\u672a\u88ab\u6b63\u786e\u5173\u95ed\u7684\u60c5\u51b5\u4e0b\u3002<\/li>\n<li><strong>\u7ba1\u7406\u590d\u6742\u6027<\/strong>\uff1a\u9700\u8981\u5bf9\u8fde\u63a5\u7684\u751f\u547d\u5468\u671f\u8fdb\u884c\u7ba1\u7406\uff0c\u589e\u52a0\u4e86\u5b9e\u73b0\u7684\u590d\u6742\u6027\u3002<\/li>\n<\/ol>\n<p><strong>\u573a\u666f\u793a\u4f8b<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u5b9e\u65f6\u901a\u4fe1<\/strong>\uff1a\u5982\u804a\u5929\u5e94\u7528\u6216\u5b9e\u65f6\u6570\u636e\u63a8\u9001\uff0c\u9891\u7e41\u7684\u8bf7\u6c42\u548c\u54cd\u5e94\u9700\u8981\u4f7f\u7528\u957f\u8fde\u63a5\u4ee5\u4fdd\u6301\u4f4e\u5ef6\u8fdf\u548c\u9ad8\u6548\u7684\u4ea4\u4e92\u3002<\/li>\n<\/ul>\n<blockquote>\n<p>\u5728\u6211\u4eec\u7cfb\u7edf\u4e2d\u4f7f\u7528\u957f\u94fe\u63a5\u8fd8\u662f\u77ed\u94fe\u63a5\u5177\u4f53\u8fd8\u8981\u6839\u636e\u4e1a\u52a1\u573a\u666f\u6765\u51b3\u5b9a\uff0c\u5982\u679c\u6211\u4eec\u670d\u52a1\u4e4b\u95f4\u7684\u4fe1\u606f\u4f20\u9012\u975e\u5e38\u9891\u7e41\uff0c\u4f7f\u7528\u957f\u94fe\u63a5\u53ef\u4ee5\u6709\u6548\u964d\u4f4e\u5efa\u7acb\u8fde\u63a5\u7684\u5f00\u9500\uff0c\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<p>\u5982\u679c\u4ea4\u4e92\u5e76\u4e0d\u9891\u7e41\u53ef\u4ee5\u9009\u7528\u77ed\u94fe\u63a5\uff0c\u80fd\u591f\u81ea\u52a8\u7ba1\u7406\u8fde\u63a5\u72b6\u6001\uff0c\u4e5f\u80fd\u4fdd\u8bc1\u5b89\u5168\u6027\u3002<\/p>\n<\/blockquote>\n<h1>\u5982\u679c\u4e00\u4e2a\u5176\u4ed6\u7cfb\u7edf\u60f3\u8981\u6211\u4eec\u7cfb\u7edf\u8ba4\u8bc1\u624d\u80fd\u542f\u52a8\uff0c\u8bf7\u8bbe\u8ba1<\/h1>\n<blockquote>\n<p>\u8fd9\u4e2a\u95ee\u9898\u5f53\u65f6\u95ee\u7684\u65f6\u5019\uff0c\u8bf4\u4e86\u4e00\u5927\u5806\u4ec0\u4e48\u53cc\u5411\u8ba4\u8bc1\u6216\u8005\u4ec0\u4e48Token\u5566\u3001JWT\u5566\uff0c\u9762\u540e\u60f3\u4e86\u60f3\u9762\u8bd5\u5b98\u6839\u672c\u4e0d\u662f\u60f3\u95ee\u8fd9\u4e2a\uff0c\u4e0d\u7136\u4e5f\u4e0d\u4f1a\u7a81\u7136\u4eceSpirng\u7684\u95ee\u7b54\u4e2d\u8df3\u51fa\u95ee\u8fd9\u4e2a\uff0c\u800c\u4e14\u9898\u76ee\u4e5f\u8bf4\u4e86<strong>\u88ab\u8ba4\u8bc1\u7cfb\u7edf\u8fde\u542f\u52a8\u90fd\u4e0d\u884c<\/strong>\u3002\u6211\u5728\u770bSpring\u5b98\u7f51\u7684\u65f6\u5019\uff0c\u770b\u5230\u4e86<strong>\u4e8b\u4ef6<\/strong>\u8fd9\u4e00\u7ae0\u624d\u660e\u767d\u8fd9\u4e2a\u9898\u7684\u7b54\u6848\u3002<\/p>\n<\/blockquote>\n<p>\u53ef\u4ee5\u901a\u8fc7\u5b9e\u73b0<code>BootstrapRegistryInitializer<\/code>\u63a5\u53e3\uff0c\u5728\u5b9e\u73b0\u7c7b\u4e2d\u8fdb\u884c\u8ba4\u8bc1\uff0c\u5982\u679c\u8ba4\u8bc1\u65e0\u6cd5\u901a\u8fc7\u5219\u9879\u76ee\u65e0\u6cd5\u542f\u52a8\uff0c\u8fd9\u4e2a\u9898\u7684\u6838\u5fc3\u76ee\u7684\u8fd8\u662fSpring\u7684\u6838\u5fc3\u5185\u5bb9\u2014\u2014<strong>\u4e8b\u4ef6\u76d1\u542c<\/strong>\u3002<\/p>\n<pre><code class=\"language-java\">@Configuration\npublic class CustomBootstrapInitializer implements BootstrapRegistryInitializer {\n\n    @Override\n    public void initialize(BootstrapRegistry bootstrapRegistry) {\n        \/\/ \u5b9e\u73b0\u4f60\u7684\u8ba4\u8bc1\u903b\u8f91,\u53ef\u4ee5\u662f\u4ece\u914d\u7f6e\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5bc6\u94a5\u6216\u8005\u5176\u4ed6\u65b9\u5f0f\n        boolean isAuthenticated = performAuthentication();\n        if (!isAuthenticated) {\n            throw new RuntimeException(&quot;Authentication failed!&quot;);\n        }\n    }\n\n    private boolean performAuthentication() {\n        \/\/ \u4f60\u7684\u8ba4\u8bc1\u4ee3\u7801\n        return true; \/\/ \u6216 false \u53d6\u51b3\u4e8e\u8ba4\u8bc1\u7ed3\u679c\n    }\n}\n<\/code><\/pre>\n<p>\u5173\u4e8e<code>BootstrapRegistryInitializer<\/code>\u8fd9\u4e2a\u7c7b\u4ee3\u8868<strong>\u611f\u77e5\u5f15\u5bfc\u521d\u59cb\u5316<\/strong>\u76d1\u542c\u5668\uff0c\u8fd9\u4e2a\u76d1\u542c\u5668\u4f1a\u5728<code>bootstrapContext<\/code>\u52a0\u8f7d\u540e\u89e6\u53d1\uff0c\u7cfb\u7edf\u4e2d\u73af\u5883\u3001\u4e0a\u4e0b\u6587\u7b49\u90fd\u662f\u5728\u8fd9\u4e4b\u540e\u52a0\u8f7d\u7684\u3002<\/p>\n<h1>\u5982\u679c\u6bcf\u4e2a\u5b57\u6bb5\u90fd\u52a0\u4e0a\u7d22\u5f15\u4f1a\u600e\u4e48\u6837\uff1f<\/h1>\n<p>\u5728\u6570\u636e\u5e93\u4e2d\u4e3a\u6bcf\u4e2a\u5b57\u6bb5\u90fd\u52a0\u4e0a\u7d22\u5f15\u53ef\u80fd\u4f1a\u5e26\u6765\u4e00\u4e9b\u4e25\u91cd\u7684\u5f0a\u7aef\uff0c\u5c3d\u7ba1\u7d22\u5f15\u53ef\u4ee5\u63d0\u9ad8\u67d0\u4e9b\u67e5\u8be2\u7684\u6027\u80fd\uff0c\u4f46\u8fc7\u591a\u7684\u7d22\u5f15\u4e5f\u4f1a\u5bfc\u81f4\u8d1f\u9762\u5f71\u54cd\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u5f0a\u7aef\u5206\u6790\uff1a<\/p>\n<p><strong>\u5199\u64cd\u4f5c\u6027\u80fd\u4e0b\u964d<\/strong><\/p>\n<p>\u7d22\u5f15\u7684\u5b58\u5728\u610f\u5473\u7740\u6bcf\u5f53\u6709\u6570\u636e\u63d2\u5165\u3001\u66f4\u65b0\u6216\u5220\u9664\u65f6\uff0c\u6570\u636e\u5e93\u4e0d\u4ec5\u9700\u8981\u4fee\u6539\u6570\u636e\u672c\u8eab\uff0c\u8fd8\u9700\u8981\u76f8\u5e94\u5730\u7ef4\u62a4\u6240\u6709\u7684\u7d22\u5f15\u3002<\/p>\n<ul>\n<li><strong>\u63d2\u5165\u64cd\u4f5c<\/strong>\uff1a\u6bcf\u6b21\u63d2\u5165\u65b0\u6570\u636e\u65f6\uff0c\u6570\u636e\u5e93\u4e0d\u4ec5\u8981\u628a\u6570\u636e\u5199\u5165\u8868\u4e2d\uff0c\u8fd8\u8981\u5728\u76f8\u5173\u7d22\u5f15\u4e2d\u63d2\u5165\u76f8\u5e94\u7684\u6761\u76ee\u3002\u5982\u679c\u6bcf\u4e2a\u5b57\u6bb5\u90fd\u6709\u7d22\u5f15\uff0c\u63d2\u5165\u64cd\u4f5c\u5c31\u4f1a\u53d8\u5f97\u975e\u5e38\u7f13\u6162\u3002<\/li>\n<li><strong>\u66f4\u65b0\u64cd\u4f5c<\/strong>\uff1a\u5982\u679c\u66f4\u65b0\u6d89\u53ca\u5230\u88ab\u7d22\u5f15\u7684\u5b57\u6bb5\uff0c\u6570\u636e\u5e93\u9700\u8981\u91cd\u65b0\u8c03\u6574\u7d22\u5f15\u4e2d\u7684\u6761\u76ee\uff0c\u7ef4\u62a4\u7d22\u5f15\u7684\u6709\u5e8f\u6027\u3002\u8fd9\u4f1a\u589e\u52a0\u66f4\u65b0\u64cd\u4f5c\u7684\u590d\u6742\u6027\u548c\u65f6\u95f4\u5f00\u9500\u3002<\/li>\n<li><strong>\u5220\u9664\u64cd\u4f5c<\/strong>\uff1a\u5220\u9664\u64cd\u4f5c\u540c\u6837\u9700\u8981\u7ef4\u62a4\u7d22\u5f15\u4e2d\u7684\u8bb0\u5f55\uff0c\u4e5f\u4f1a\u964d\u4f4e\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p><strong>\u5b58\u50a8\u7a7a\u95f4\u7684\u6d88\u8017<\/strong><\/p>\n<p>\u6bcf\u4e2a\u7d22\u5f15\u90fd\u5360\u7528\u989d\u5916\u7684\u5b58\u50a8\u7a7a\u95f4\u3002\u4e3a\u6bcf\u4e2a\u5b57\u6bb5\u90fd\u521b\u5efa\u7d22\u5f15\u4f1a\u663e\u8457\u589e\u52a0\u6570\u636e\u5e93\u7684\u5b58\u50a8\u9700\u6c42\uff0c\u7279\u522b\u662f\u5728\u5927\u8868\u4e2d\uff0c\u8fd9\u79cd\u5b58\u50a8\u7a7a\u95f4\u7684\u5f00\u9500\u4f1a\u975e\u5e38\u663e\u8457\u3002\u7d22\u5f15\u8d8a\u591a\uff0c\u5360\u7528\u7684\u78c1\u76d8\u7a7a\u95f4\u8d8a\u5927\u3002<\/p>\n<ul>\n<li><strong>\u7d22\u5f15\u6587\u4ef6\u5927\u5c0f<\/strong>\uff1a\u6570\u636e\u5e93\u4e2d\u7684\u6bcf\u4e2a\u7d22\u5f15\u901a\u5e38\u4f1a\u751f\u6210\u72ec\u7acb\u7684\u6587\u4ef6\u6216\u7ed3\u6784\u6765\u5b58\u50a8\uff0c\u8fd9\u4e9b\u6587\u4ef6\u4f1a\u5360\u7528\u78c1\u76d8\u8d44\u6e90\uff0c\u5bfc\u81f4\u5b58\u50a8\u6210\u672c\u589e\u52a0\u3002<\/li>\n<li><strong>\u5185\u5b58\u5f00\u9500<\/strong>\uff1a\u6570\u636e\u5e93\u5728\u67e5\u8be2\u65f6\u9700\u8981\u5c06\u7d22\u5f15\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u4ee5\u52a0\u901f\u68c0\u7d22\u64cd\u4f5c\u3002\u5927\u91cf\u7684\u7d22\u5f15\u4f1a\u5360\u7528\u66f4\u591a\u7684\u5185\u5b58\u8d44\u6e90\uff0c\u4ece\u800c\u964d\u4f4e\u6570\u636e\u5e93\u7684\u6574\u4f53\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p><strong>\u67e5\u8be2\u4f18\u5316\u5668\u7684\u56f0\u60d1<\/strong><\/p>\n<p>\u73b0\u4ee3\u6570\u636e\u5e93\u7cfb\u7edf\u4f7f\u7528\u67e5\u8be2\u4f18\u5316\u5668\u6765\u786e\u5b9a\u6700\u4f18\u7684\u67e5\u8be2\u6267\u884c\u8ba1\u5212\u3002\u7136\u800c\uff0c\u5982\u679c\u8868\u4e2d\u5b58\u5728\u8fc7\u591a\u7684\u7d22\u5f15\uff0c\u4f18\u5316\u5668\u53ef\u80fd\u4f1a\u82b1\u8d39\u66f4\u591a\u7684\u65f6\u95f4\u6765\u5206\u6790\u548c\u9009\u62e9\u9002\u5408\u7684\u7d22\u5f15\uff0c\u751a\u81f3\u53ef\u80fd\u505a\u51fa\u9519\u8bef\u7684\u9009\u62e9\u3002<\/p>\n<ul>\n<li><strong>\u8fc7\u5ea6\u9009\u62e9<\/strong>\uff1a\u4f18\u5316\u5668\u9700\u8981\u4e3a\u6bcf\u4e2a\u67e5\u8be2\u786e\u5b9a\u6700\u4f18\u7684\u7d22\u5f15\u8def\u5f84\u3002\u8fc7\u591a\u7684\u7d22\u5f15\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4f18\u5316\u5668\u5728\u9009\u62e9\u6700\u4f73\u7d22\u5f15\u65f6\u9047\u5230\u56f0\u96be\uff0c\u4ece\u800c\u9009\u62e9\u4e0d\u9002\u5408\u7684\u7d22\u5f15\u8def\u5f84\uff0c\u5bfc\u81f4\u67e5\u8be2\u6027\u80fd\u4e0b\u964d\u3002<\/li>\n<li><strong>\u7d22\u5f15\u51b2\u7a81<\/strong>\uff1a\u591a\u4e2a\u7d22\u5f15\u53ef\u80fd\u4f1a\u5f7c\u6b64\u7ade\u4e89\uff0c\u7279\u522b\u662f\u5728\u591a\u5217\u67e5\u8be2\u65f6\uff0c\u6570\u636e\u5e93\u53ef\u80fd\u65e0\u6cd5\u660e\u786e\u9009\u62e9\u6700\u4f73\u7684\u7ec4\u5408\u7d22\u5f15\uff0c\u5bfc\u81f4\u4e0d\u5fc5\u8981\u7684\u5168\u8868\u626b\u63cf\u6216\u975e\u6700\u4f18\u67e5\u8be2\u8def\u5f84\u3002<\/li>\n<\/ul>\n<p><strong>\u7ef4\u62a4\u4e0e\u7ba1\u7406\u590d\u6742\u6027<\/strong><\/p>\n<p>\u7d22\u5f15\u9700\u8981\u5b9a\u671f\u8fdb\u884c\u7ef4\u62a4\uff0c\u6bd4\u5982\u91cd\u5efa\u6216\u91cd\u7ec4\uff0c\u5c24\u5176\u662f\u5f53\u7d22\u5f15\u788e\u7247\u5316\u4e25\u91cd\u65f6\uff08\u63d2\u5165\u3001\u5220\u9664\u64cd\u4f5c\u9891\u7e41\u5bfc\u81f4\u7684\u788e\u7247\uff09\u3002\u5f53\u4e3a\u6bcf\u4e2a\u5b57\u6bb5\u90fd\u521b\u5efa\u7d22\u5f15\u65f6\uff0c\u7ba1\u7406\u8fd9\u4e9b\u7d22\u5f15\u5c06\u53d8\u5f97\u975e\u5e38\u590d\u6742\u548c\u8017\u65f6\u3002<\/p>\n<ul>\n<li><strong>\u5b9a\u671f\u7ef4\u62a4<\/strong>\uff1a\u5927\u91cf\u7684\u7d22\u5f15\u9700\u8981\u9891\u7e41\u7684\u7ef4\u62a4\u64cd\u4f5c\uff0c\u8fd9\u4f1a\u5360\u7528\u66f4\u591a\u7684\u8d44\u6e90\u5e76\u589e\u52a0\u7ba1\u7406\u8d1f\u62c5\u3002<\/li>\n<li><strong>\u6027\u80fd\u76d1\u63a7<\/strong>\uff1a\u7ba1\u7406\u5458\u9700\u8981\u5b9a\u671f\u68c0\u67e5\u54ea\u4e9b\u7d22\u5f15\u662f\u6709\u6548\u7684\uff0c\u54ea\u4e9b\u662f\u5197\u4f59\u7684\u6216\u8005\u672a\u88ab\u4f7f\u7528\u7684\u3002\u7d22\u5f15\u7684\u8fc7\u5ea6\u589e\u52a0\u4f1a\u4f7f\u5f97\u8fd9\u4e2a\u76d1\u63a7\u8fc7\u7a0b\u66f4\u52a0\u7e41\u7410\u3002<\/li>\n<\/ul>\n<p><strong>\u5197\u4f59\u7d22\u5f15\u7684\u53ef\u80fd\u6027<\/strong><\/p>\n<p>\u5982\u679c\u4e3a\u6bcf\u4e2a\u5b57\u6bb5\u90fd\u52a0\u4e0a\u7d22\u5f15\uff0c\u8bb8\u591a\u7d22\u5f15\u53ef\u80fd\u5728\u5b9e\u9645\u4f7f\u7528\u4e2d\u662f\u5197\u4f59\u7684\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u8868\u4e2d\u6709\u590d\u5408\u7d22\u5f15\uff08\u591a\u5217\u7d22\u5f15\uff09\uff0c\u5355\u72ec\u4e3a\u6bcf\u5217\u518d\u521b\u5efa\u7d22\u5f15\u662f\u6ca1\u6709\u5fc5\u8981\u7684\uff0c\u56e0\u4e3a\u590d\u5408\u7d22\u5f15\u5df2\u7ecf\u53ef\u4ee5\u6ee1\u8db3\u591a\u4e2a\u5b57\u6bb5\u7684\u67e5\u8be2\u9700\u6c42\u3002<\/p>\n<ul>\n<li><strong>\u91cd\u590d\u6027<\/strong>\uff1a\u4e00\u4e9b\u590d\u5408\u67e5\u8be2\u53ef\u80fd\u5df2\u7ecf\u88ab\u73b0\u6709\u7684\u7ec4\u5408\u7d22\u5f15\u8986\u76d6\uff0c\u518d\u4e3a\u5355\u72ec\u7684\u5217\u52a0\u7d22\u5f15\u4f1a\u53d8\u5f97\u5197\u4f59\uff0c\u65e2\u5360\u7528\u5b58\u50a8\u7a7a\u95f4\u53c8\u5f71\u54cd\u6027\u80fd\u3002<\/li>\n<li><strong>\u65e0\u7528\u7d22\u5f15<\/strong>\uff1a\u67d0\u4e9b\u5b57\u6bb5\u7684\u7d22\u5f15\u53ef\u80fd\u5728\u5b9e\u9645\u67e5\u8be2\u4e2d\u51e0\u4e4e\u4ece\u672a\u4f7f\u7528\uff0c\u8fd9\u6837\u7684\u7d22\u5f15\u4e0d\u4ec5\u6d6a\u8d39\u7a7a\u95f4\uff0c\u8fd8\u62d6\u6162\u5199\u64cd\u4f5c\u7684\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p><strong>\u4f18\u5316\u5e76\u4e0d\u662f\u7ebf\u6027\u9012\u589e\u7684<\/strong><\/p>\n<p>\u5e76\u4e0d\u662f\u6bcf\u4e2a\u5b57\u6bb5\u7684\u7d22\u5f15\u90fd\u4f1a\u5e26\u6765\u663e\u8457\u7684\u6027\u80fd\u63d0\u5347\u3002\u67d0\u4e9b\u5b57\u6bb5\u7684\u7d22\u5f15\u5728\u67e5\u8be2\u4e2d\u53ef\u80fd\u5f88\u5c11\u4f7f\u7528\uff0c\u751a\u81f3\u6709\u4e9b\u5b57\u6bb5\u7684\u6570\u636e\u7279\u6027\uff08\u4f8b\u5982\u9ad8\u91cd\u590d\u6027\u6216\u4f4e\u9009\u62e9\u6027\uff09\u4f7f\u5f97\u7d22\u5f15\u7684\u6548\u679c\u5fae\u4e4e\u5176\u5fae\u3002<\/p>\n<ul>\n<li><strong>\u4f4e\u9009\u62e9\u6027\u5b57\u6bb5\u7684\u7d22\u5f15<\/strong>\uff1a\u5982\u679c\u5b57\u6bb5\u7684\u53d6\u503c\u8303\u56f4\u975e\u5e38\u5c0f\uff08\u5982\u6027\u522b\u5b57\u6bb5\uff0c\u53ea\u6709\u201c\u7537\u201d\u548c\u201c\u5973\u201d\u4e24\u79cd\u53ef\u80fd\u503c\uff09\uff0c\u4e3a\u8fd9\u4e9b\u5b57\u6bb5\u52a0\u7d22\u5f15\u53ef\u80fd\u4e0d\u4f1a\u5e26\u6765\u663e\u8457\u7684\u6027\u80fd\u63d0\u5347\uff0c\u53cd\u800c\u53ef\u80fd\u589e\u52a0\u989d\u5916\u7684\u8d1f\u62c5\u3002<\/li>\n<\/ul>\n<p><strong>\u9501\u7ade\u4e89\u589e\u52a0<\/strong><\/p>\n<p>\u5f53\u6570\u636e\u5e93\u6267\u884c\u5e76\u53d1\u64cd\u4f5c\u65f6\uff0c\u8fc7\u591a\u7684\u7d22\u5f15\u4f1a\u589e\u52a0\u9501\u7684\u7ade\u4e89\u3002\u5728\u5199\u5165\u6570\u636e\u65f6\uff0c\u6570\u636e\u5e93\u9700\u8981\u9501\u4f4f\u76f8\u5173\u7684\u7d22\u5f15\u6761\u76ee\uff0c\u786e\u4fdd\u6570\u636e\u4e00\u81f4\u6027\u3002\u7d22\u5f15\u8d8a\u591a\uff0c\u9501\u7ade\u4e89\u7684\u53ef\u80fd\u6027\u8d8a\u5927\uff0c\u5c24\u5176\u5728\u9ad8\u5e76\u53d1\u73af\u5883\u4e0b\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u6574\u4f53\u6027\u80fd\u7684\u4e0b\u964d\u3002<\/p>\n<h1>\u4e00\u4e2a\u6587\u4ef6\u6bcf\u4e00\u884c\u90fd\u662f\u4e00\u4e2a\u6574\u5f62\u7684\u6570\u5b57\uff0c\u4ece\u8fd9\u4e2a\u6587\u4ef6\u4e2d\u627e\u51fa\u6700\u5927\u7684 10 \u6570\u5b57\uff0c\u600e\u4e48\u505a<\/h1>\n<p>\u53ef\u4ee5\u4f7f\u7528<strong>\u6700\u5c0f\u5806<\/strong>\u6765\u505a\u3002<\/p>\n<div  class='collapse-block shadow-sm collapse-block-danger collapsed hide-border-left'><div class='collapse-block-title'><span class='collapse-block-title-inner'>\u6700\u5c0f\u5806\u5b9a\u4e49<\/span><i class='collapse-icon fa fa-angle-down'><\/i><\/div><div class='collapse-block-body' style='display:none;'><\/p>\n<p>\u6700\u5c0f\u5806\uff08Min Heap\uff09\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u5176\u4e2d\u6bcf\u4e2a\u7236\u8282\u70b9\u7684\u503c\u90fd\u5c0f\u4e8e\u6216\u7b49\u4e8e\u5176\u5b50\u8282\u70b9\u7684\u503c\u3002\u8fd9\u79cd\u7ed3\u6784\u4f7f\u5f97\u6700\u5c0f\u5806\u7684\u6839\u8282\u70b9\u603b\u662f\u6240\u6709\u8282\u70b9\u4e2d\u6700\u5c0f\u7684\u4e00\u4e2a\u3002\u6700\u5c0f\u5806\u5177\u6709\u4ee5\u4e0b\u5173\u952e\u7279\u6027\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u7236\u8282\u70b9\u5c0f\u4e8e\u5b50\u8282\u70b9<\/strong> \uff1a\u5728\u6700\u5c0f\u5806\u4e2d\uff0c\u6bcf\u4e2a\u7236\u8282\u70b9\u7684\u503c\u90fd\u5c0f\u4e8e\u6216\u7b49\u4e8e\u5176\u5b50\u8282\u70b9\u7684\u503c\u3002\u8fd9\u610f\u5473\u7740\u4ece\u6839\u8282\u70b9\u5230\u4efb\u610f\u53f6\u8282\u70b9\u7684\u8def\u5f84\u4e0a\uff0c\u8282\u70b9\u7684\u503c\u662f\u9012\u589e\u7684\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5b8c\u5168\u4e8c\u53c9\u6811<\/strong> \uff1a\u6700\u5c0f\u5806\u662f\u4e00\u79cd\u5b8c\u5168\u4e8c\u53c9\u6811\uff0c\u8fd9\u610f\u5473\u7740\u9664\u4e86\u6700\u540e\u4e00\u5c42\u5916\uff0c\u5176\u4ed6\u5c42\u7684\u8282\u70b9\u90fd\u662f\u6ee1\u7684\uff0c\u5e76\u4e14\u6700\u540e\u4e00\u5c42\u7684\u8282\u70b9\u5c3d\u53ef\u80fd\u5730\u9760\u5de6\u6392\u5217\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u9ad8\u6548\u7684\u64cd\u4f5c<\/strong> \uff1a\u6700\u5c0f\u5806\u652f\u6301\u9ad8\u6548\u7684\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u3002\u63d2\u5165\u4e00\u4e2a\u65b0\u5143\u7d20\u65f6\uff0c\u5c06\u5176\u653e\u5728\u5806\u7684\u672b\u5c3e\uff0c\u7136\u540e\u901a\u8fc7\u201c\u4e0a\u6d6e\u201d\u64cd\u4f5c\u5c06\u5176\u8c03\u6574\u5230\u6b63\u786e\u7684\u4f4d\u7f6e\u3002\u5220\u9664\u6700\u5c0f\u5143\u7d20\uff08\u5373\u6839\u8282\u70b9\uff09\u65f6\uff0c\u5c06\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u79fb\u52a8\u5230\u6839\u8282\u70b9\uff0c\u7136\u540e\u901a\u8fc7\u201c\u4e0b\u6c89\u201d\u64cd\u4f5c\u5c06\u5176\u8c03\u6574\u5230\u6b63\u786e\u7684\u4f4d\u7f6e\u3002\u8fd9\u4e24\u79cd\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u5747\u4e3aO(log n)\u3002<\/p>\n<\/li>\n<li>\n<p><strong>\u5e94\u7528\u573a\u666f<\/strong> \uff1a\u6700\u5c0f\u5806\u5e38\u7528\u4e8e\u9700\u8981\u9891\u7e41\u8bbf\u95ee\u6700\u5c0f\u5143\u7d20\u7684\u573a\u666f\uff0c\u4f8b\u5982\u4f18\u5148\u961f\u5217\u3001Dijkstra\u7b97\u6cd5\u3001\u6700\u5c0f\u751f\u6210\u6811\u7b97\u6cd5\u7b49\u3002<\/p>\n<\/li>\n<\/ol>\n<h3>\u6700\u5c0f\u5806\u7684\u64cd\u4f5c<\/h3>\n<ol>\n<li>\n<p><strong>\u63d2\u5165\u5143\u7d20<\/strong> \uff1a<\/p>\n<ul>\n<li>\u5c06\u65b0\u5143\u7d20\u63d2\u5165\u5806\u7684\u672b\u5c3e\u3002<\/li>\n<li>\u901a\u8fc7\u201c\u4e0a\u6d6e\u201d\u64cd\u4f5c\uff08\u5c06\u65b0\u5143\u7d20\u4e0e\u5176\u7236\u8282\u70b9\u6bd4\u8f83\u5e76\u4ea4\u6362\uff0c\u76f4\u5230\u6ee1\u8db3\u6700\u5c0f\u5806\u7684\u6027\u8d28\uff09\u5c06\u65b0\u5143\u7d20\u8c03\u6574\u5230\u6b63\u786e\u7684\u4f4d\u7f6e\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5220\u9664\u6700\u5c0f\u5143\u7d20<\/strong> \uff1a<\/p>\n<ul>\n<li>\u5c06\u6839\u8282\u70b9\uff08\u6700\u5c0f\u5143\u7d20\uff09\u79fb\u9664\u3002<\/li>\n<li>\u5c06\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u79fb\u52a8\u5230\u6839\u8282\u70b9\u3002<\/li>\n<li>\u901a\u8fc7\u201c\u4e0b\u6c89\u201d\u64cd\u4f5c\uff08\u5c06\u6839\u8282\u70b9\u4e0e\u5176\u5b50\u8282\u70b9\u6bd4\u8f83\u5e76\u4ea4\u6362\uff0c\u76f4\u5230\u6ee1\u8db3\u6700\u5c0f\u5806\u7684\u6027\u8d28\uff09\u5c06\u6839\u8282\u70b9\u8c03\u6574\u5230\u6b63\u786e\u7684\u4f4d\u7f6e\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u6784\u5efa\u6700\u5c0f\u5806<\/strong> \uff1a<\/p>\n<ul>\n<li>\u4ece\u6570\u7ec4\u6216\u5217\u8868\u4e2d\u6784\u5efa\u6700\u5c0f\u5806\u65f6\uff0c\u901a\u5e38\u4ece\u6700\u540e\u4e00\u4e2a\u975e\u53f6\u8282\u70b9\u5f00\u59cb\uff0c\u9010\u4e2a\u5bf9\u5176\u8fdb\u884c\u201c\u4e0b\u6c89\u201d\u64cd\u4f5c\uff0c\u76f4\u5230\u6839\u8282\u70b9\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<h3>\u793a\u4f8b<\/h3>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4ee5\u4e0b\u6570\u7ec4\uff1a[4, 10, 3, 5, 1]\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5176\u6784\u5efa\u6210\u4e00\u4e2a\u6700\u5c0f\u5806\uff1a<\/p>\n<ol>\n<li><strong>\u521d\u59cb\u72b6\u6001<\/strong> \uff1a[4, 10, 3, 5, 1]<\/li>\n<li><strong>\u6784\u5efa\u6700\u5c0f\u5806<\/strong> \uff1a\n<ul>\n<li>\u4ece\u6700\u540e\u4e00\u4e2a\u975e\u53f6\u8282\u70b9\uff08\u5373\u7d22\u5f15\u4e3a2\u7684\u8282\u70b9\uff0c\u503c\u4e3a3\uff09\u5f00\u59cb\uff0c\u9010\u4e2a\u8fdb\u884c\u201c\u4e0b\u6c89\u201d\u64cd\u4f5c\u3002<\/li>\n<li>\u6700\u7ec8\u5f97\u5230\u7684\u6700\u5c0f\u5806\u4e3a\uff1a[1, 4, 3, 5, 10]<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p>\u5728\u8fd9\u4e2a\u6700\u5c0f\u5806\u4e2d\uff0c\u6839\u8282\u70b9\uff08\u503c\u4e3a1\uff09\u662f\u6700\u5c0f\u7684\u5143\u7d20\uff0c\u5e76\u4e14\u6bcf\u4e2a\u7236\u8282\u70b9\u7684\u503c\u90fd\u5c0f\u4e8e\u6216\u7b49\u4e8e\u5176\u5b50\u8282\u70b9\u7684\u503c\u3002<\/p>\n<p><\/div><\/div>\n<ol>\n<li><strong>\u521d\u59cb\u5316\u6700\u5c0f\u5806<\/strong> \uff1a\u9996\u5148\uff0c\u4f60\u521b\u5efa\u4e00\u4e2a\u6700\u5c0f\u5806\uff0c\u5e76\u521d\u59cb\u5316\u4e3a\u7a7a\u3002\u8fd9\u4e2a\u5806\u5c06\u7528\u4e8e\u5b58\u50a8\u5f53\u524d\u627e\u5230\u7684\u6700\u5927\u768410\u4e2a\u6570\u5b57\u3002<\/li>\n<li><strong>\u9010\u4e2a\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u6570\u5b57<\/strong> \uff1a\u4f60\u9010\u4e2a\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u6570\u5b57\uff0c\u5e76\u5c06\u6bcf\u4e2a\u6570\u5b57\u4e0e\u6700\u5c0f\u5806\u7684\u6839\u8282\u70b9\uff08\u5373\u5f53\u524d\u5806\u4e2d\u6700\u5c0f\u7684\u6570\u5b57\uff09\u8fdb\u884c\u6bd4\u8f83\u3002<\/li>\n<li><strong>\u7ef4\u62a4\u5806\u7684\u5927\u5c0f<\/strong> \uff1a\u5982\u679c\u5f53\u524d\u6570\u5b57\u5927\u4e8e\u5806\u7684\u6839\u8282\u70b9\uff0c\u5219\u5c06\u6839\u8282\u70b9\u66ff\u6362\u4e3a\u5f53\u524d\u6570\u5b57\uff0c\u5e76\u91cd\u65b0\u8c03\u6574\u5806\u4ee5\u4fdd\u6301\u6700\u5c0f\u5806\u7684\u6027\u8d28\u3002\u5982\u679c\u5f53\u524d\u6570\u5b57\u5c0f\u4e8e\u6216\u7b49\u4e8e\u6839\u8282\u70b9\uff0c\u5219\u5ffd\u7565\u8be5\u6570\u5b57\u3002<\/li>\n<li><strong>\u91cd\u590d\u76f4\u5230\u6587\u4ef6\u7ed3\u675f<\/strong> \uff1a\u91cd\u590d\u4e0a\u8ff0\u8fc7\u7a0b\uff0c\u76f4\u5230\u4f60\u8bfb\u53d6\u5b8c\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u6570\u5b57\u3002\u6b64\u65f6\uff0c\u6700\u5c0f\u5806\u4e2d\u5c06\u5305\u542b\u6587\u4ef6\u4e2d\u6700\u5927\u768410\u4e2a\u6570\u5b57\u3002<\/li>\n<\/ol>\n<p><strong>\u5177\u4f53\u6b65\u9aa4<\/strong><\/p>\n<ol>\n<li><strong>\u521d\u59cb\u5316\u6700\u5c0f\u5806<\/strong> \uff1a\u521b\u5efa\u4e00\u4e2a\u5927\u5c0f\u4e3a10\u7684\u6700\u5c0f\u5806\u3002<\/li>\n<li><strong>\u9010\u4e2a\u8bfb\u53d6\u6570\u5b57<\/strong> \uff1a\n<ul>\n<li>\u5982\u679c\u5806\u672a\u6ee1\uff08\u5373\u5806\u4e2d\u7684\u5143\u7d20\u6570\u91cf\u5c0f\u4e8e10\uff09\uff0c\u76f4\u63a5\u5c06\u5f53\u524d\u6570\u5b57\u63d2\u5165\u5806\u4e2d\uff0c\u5e76\u8c03\u6574\u5806\u3002<\/li>\n<li>\u5982\u679c\u5806\u5df2\u6ee1\uff0c\u5c06\u5f53\u524d\u6570\u5b57\u4e0e\u5806\u7684\u6839\u8282\u70b9\u8fdb\u884c\u6bd4\u8f83\uff1a\n<ul>\n<li>\u5982\u679c\u5f53\u524d\u6570\u5b57\u5927\u4e8e\u6839\u8282\u70b9\uff0c\u5219\u66ff\u6362\u6839\u8282\u70b9\uff0c\u5e76\u8c03\u6574\u5806\u3002<\/li>\n<li>\u5982\u679c\u5f53\u524d\u6570\u5b57\u5c0f\u4e8e\u6216\u7b49\u4e8e\u6839\u8282\u70b9\uff0c\u5219\u5ffd\u7565\u8be5\u6570\u5b57\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u8c03\u6574\u5806<\/strong> \uff1a\u6bcf\u6b21\u66ff\u6362\u6839\u8282\u70b9\u540e\uff0c\u901a\u8fc7\u4e0b\u6c89\u64cd\u4f5c\u91cd\u65b0\u8c03\u6574\u5806\uff0c\u786e\u4fdd\u5806\u7684\u6027\u8d28\uff08\u5373\u6bcf\u4e2a\u7236\u8282\u70b9\u90fd\u5c0f\u4e8e\u5176\u5b50\u8282\u70b9\uff09\u5f97\u4ee5\u4fdd\u6301\u3002<\/li>\n<li><strong>\u5b8c\u6210\u8bfb\u53d6<\/strong> \uff1a\u5f53\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u6570\u5b57\u90fd\u88ab\u8bfb\u53d6\u5e76\u5904\u7406\u540e\uff0c\u6700\u5c0f\u5806\u4e2d\u5c06\u5305\u542b\u6587\u4ef6\u4e2d\u6700\u5927\u768410\u4e2a\u6570\u5b57\u3002<\/li>\n<\/ol>\n<pre><code class=\"language-java\">import java.util.Arrays;\n\npublic class MinHeap {\n    private int[] heap;\n    private int size;\n    private int capacity;\n\n    public MinHeap(int capacity) {\n        this.capacity = capacity;\n        this.heap = new int[capacity];\n        this.size = 0;\n    }\n\n    private int parent(int index) {\n        return (index - 1) \/ 2;\n    }\n\n    private int leftChild(int index) {\n        return 2 * index + 1;\n    }\n\n    private int rightChild(int index) {\n        return 2 * index + 2;\n    }\n\n    private void swap(int i, int j) {\n        int temp = heap[i];\n        heap[i] = heap[j];\n        heap[j] = temp;\n    }\n\n    public void insert(int value) {\n        if (size == capacity) {\n            if (value &gt; heap[0]) {\n                heap[0] = value;\n                heapifyDown(0);\n            }\n        } else {\n            heap[size] = value;\n            size++;\n            heapifyUp(size - 1);\n        }\n    }\n\n    private void heapifyUp(int index) {\n        while (index &gt; 0 &amp;&amp; heap[index] &lt; heap[parent(index)]) {\n            swap(index, parent(index));\n            index = parent(index);\n        }\n    }\n\n    private void heapifyDown(int index) {\n        int smallest = index;\n        int left = leftChild(index);\n        int right = rightChild(index);\n\n        if (left &lt; size &amp;&amp; heap[left] &lt; heap[smallest]) {\n            smallest = left;\n        }\n\n        if (right &lt; size &amp;&amp; heap[right] &lt; heap[smallest]) {\n            smallest = right;\n        }\n\n        if (smallest != index) {\n            swap(index, smallest);\n            heapifyDown(smallest);\n        }\n    }\n\n    public int[] getTopK() {\n        int[] result = Arrays.copyOf(heap, size);\n        Arrays.sort(result);\n        return result;\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">import java.io.BufferedReader;\nimport java.io.FileReader;\nimport java.io.IOException;\n\npublic class TopKNumbers {\n    public static void main(String[] args) {\n        String filePath = &quot;numbers.txt&quot;; \/\/ \u6587\u4ef6\u8def\u5f84\n        int k = 10; \/\/ \u627e\u51fa\u6700\u5927\u768410\u4e2a\u6570\u5b57\n\n        MinHeap minHeap = new MinHeap(k);\n\n        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {\n            String line;\n            while ((line = br.readLine()) != null) {\n                int number = Integer.parseInt(line);\n                minHeap.insert(number);\n            }\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n\n        int[] topK = minHeap.getTopK();\n        System.out.println(&quot;\u6700\u5927\u7684 &quot; + k + &quot; \u4e2a\u6570\u5b57\u662f:&quot;);\n        for (int num : topK) {\n            System.out.println(num);\n        }\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd8\u6709\u597d\u51e0\u4e2a\u5b9e\u5728\u60f3\u4e0d\u8d77\u6765\u4e86 \/(\u3112o\u3112)\/~~ openfeign \u7684\u6267\u884c\u539f\u7406 \u9996\u5148\u901a\u8fc7@EnableFeign [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[32],"class_list":["post-81","post","type-post","status-publish","format-standard","hentry","category-interview","tag-32"],"_links":{"self":[{"href":"https:\/\/blog.eastfond.com\/index.php?rest_route=\/wp\/v2\/posts\/81","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.eastfond.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.eastfond.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.eastfond.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.eastfond.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=81"}],"version-history":[{"count":7,"href":"https:\/\/blog.eastfond.com\/index.php?rest_route=\/wp\/v2\/posts\/81\/revisions"}],"predecessor-version":[{"id":430,"href":"https:\/\/blog.eastfond.com\/index.php?rest_route=\/wp\/v2\/posts\/81\/revisions\/430"}],"wp:attachment":[{"href":"https:\/\/blog.eastfond.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.eastfond.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.eastfond.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}