{"id":121378,"date":"2026-11-02T16:32:43","date_gmt":"2026-11-02T08:32:43","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/121378.html"},"modified":"2026-11-02T16:32:43","modified_gmt":"2026-11-02T08:32:43","slug":"java-stream-%e8%bd%ac-map-%e6%8a%a5-duplicate-key%ef%bc%9a%e6%98%8e%e7%a1%ae%e9%87%8d%e5%a4%8d%e9%94%ae%e7%ad%96%e7%95%a5","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/121378.html","title":{"rendered":"Java Stream \u8f6c Map \u62a5 Duplicate key\uff1a\u660e\u786e\u91cd\u590d\u952e\u7b56\u7565"},"content":{"rendered":"<p>\u628a\u67e5\u8be2\u5217\u8868\u8f6c\u6362\u6210 Map \u540e&#xff0c;Java \u629b\u51fa Duplicate key&#xff0c;\u63a5\u53e3\u76f4\u63a5\u53d8\u6210 500\u3002\u7ed9 toMap \u968f\u624b\u52a0\u4e00\u4e2a\u4fdd\u7559\u7b2c\u4e00\u6761\u7684\u5408\u5e76\u51fd\u6570&#xff0c;\u867d\u7136\u53ef\u80fd\u4e0d\u518d\u62a5\u9519&#xff0c;\u5374\u4e5f\u53ef\u80fd\u4e22\u6389\u6709\u6548\u8bb0\u5f55\u3002\u672c\u7bc7\u4ee5 JDK 17 \u4e3a\u8303\u56f4&#xff0c;\u7528\u8bbe\u5907\u67e5\u8be2\u7ed3\u679c\u8bf4\u660e\u952e\u51b2\u7a81\u7684\u542b\u4e49&#xff0c;\u6309\u4e00\u5bf9\u4e00\u4e0e\u4e00\u5bf9\u591a\u5173\u7cfb\u9009\u62e9\u4fee\u590d&#xff0c;\u5e76\u7ed9\u51fa\u53ef\u533a\u5206\u6570\u636e\u7b56\u7565\u7684\u9a8c\u8bc1\u6837\u672c\u3002<\/p>\n<h3>\u4e00\u3001\u5148\u786e\u8ba4\u8fd9\u4e2a\u952e\u771f\u7684\u552f\u4e00\u5417<\/h3>\n<p>Map \u4e2d\u4e00\u4e2a\u952e\u53ea\u5bf9\u5e94\u4e00\u4e2a\u503c\u3002Collectors.toMap \u7684\u5e38\u7528\u4e24\u53c2\u6570\u5f62\u5f0f\u6ca1\u6709\u544a\u8bc9\u7a0b\u5e8f\u9047\u5230\u91cd\u590d\u952e\u8be5\u600e\u4e48\u529e&#xff0c;\u56e0\u6b64\u9047\u5230\u51b2\u7a81\u4f1a\u629b\u51fa\u5f02\u5e38\u3002\u9519\u8bef\u4e0d\u662f Stream \u65e0\u6cd5\u5904\u7406\u5217\u8868&#xff0c;\u800c\u662f\u8f93\u5165\u5173\u7cfb\u4e0e\u76ee\u6807\u5bb9\u5668\u7ea6\u675f\u4e0d\u4e00\u81f4\u3002<\/p>\n<p>\u5047\u8bbe\u8bbe\u5907\u67e5\u8be2\u7ed3\u679c\u5305\u542b\u540c\u4e00\u8bbe\u5907\u7684\u4e24\u6b21\u501f\u7528\u8bb0\u5f55&#xff0c;\u7528\u8bbe\u5907\u7f16\u53f7\u4f5c\u4e3a Map \u952e\u5c31\u4f1a\u51b2\u7a81\u3002\u8fd9\u53ef\u80fd\u662f\u6b63\u5e38\u4e00\u5bf9\u591a&#xff0c;\u4e5f\u53ef\u80fd\u662f JOIN \u6269\u884c\u6216\u6e90\u6570\u636e\u91cd\u590d\u3002\u4fee\u590d\u524d\u5148\u56de\u7b54&#xff1a;\u6bcf\u4e2a\u8bbe\u5907\u5e94\u8be5\u53ea\u6709\u4e00\u6761\u7ed3\u679c&#xff0c;\u8fd8\u662f\u672c\u6765\u5c31\u6709\u591a\u6761&#xff1f;\u5982\u679c\u56de\u7b54\u4e0d\u6e05\u695a&#xff0c;\u4efb\u4f55\u5408\u5e76\u89c4\u5219\u90fd\u53ef\u80fd\u63a9\u76d6\u95ee\u9898\u3002<\/p>\n<table>\n<tr>\u771f\u5b9e\u5173\u7cfb\u76ee\u6807\u7ed3\u6784\u5904\u7406\u65b9\u5411<\/tr>\n<tbody>\n<tr>\n<td>\u7f16\u53f7\u552f\u4e00<\/td>\n<td>Map \u7f16\u53f7\u5230\u8bb0\u5f55<\/td>\n<td>\u67e5\u6e05\u91cd\u590d\u6765\u6e90\u5e76\u62d2\u7edd\u5f02\u5e38<\/td>\n<\/tr>\n<tr>\n<td>\u6bcf\u4e2a\u8bbe\u5907\u591a\u6761\u8bb0\u5f55<\/td>\n<td>Map \u7f16\u53f7\u5230\u5217\u8868<\/td>\n<td>\u5206\u7ec4\u4fdd\u7559\u6240\u6709\u503c<\/td>\n<\/tr>\n<tr>\n<td>\u660e\u786e\u53ea\u4fdd\u7559\u9996\u6b21\u8bb0\u5f55<\/td>\n<td>Map \u7f16\u53f7\u5230\u9996\u6b21\u503c<\/td>\n<td>\u4f7f\u7528\u6709\u987a\u5e8f\u4f9d\u636e\u7684\u5408\u5e76\u7b56\u7565<\/td>\n<\/tr>\n<tr>\n<td>\u660e\u786e\u9009\u62e9\u6700\u65b0\u7248\u672c<\/td>\n<td>Map \u7f16\u53f7\u5230\u6700\u65b0\u503c<\/td>\n<td>\u6bd4\u8f83\u7248\u672c\u5e76\u89c4\u5b9a\u5e76\u5217\u89c4\u5219<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u4e0d\u8981\u628a\u201c\u5148\u4e0d\u62a5\u9519\u201d\u5f53\u6210\u4e1a\u52a1\u542b\u4e49\u3002\u5c24\u5176\u662f\u91d1\u989d\u3001\u5e93\u5b58\u4e0e\u6743\u9650\u8bb0\u5f55&#xff0c;\u6084\u6084\u820d\u5f03\u4e00\u6761\u53ef\u80fd\u6bd4\u5f02\u5e38\u66f4\u96be\u53d1\u73b0\u3002<\/p>\n<h3>\u4e8c\u3001\u7528\u4e09\u6761\u6570\u636e\u590d\u73b0\u952e\u51b2\u7a81<\/h3>\n<p>\u4e0b\u9762\u662f\u72ec\u7acb\u6559\u5b66\u6570\u636e&#xff0c;\u5b57\u6bb5\u53ea\u6709\u7f16\u53f7\u4e0e\u540d\u79f0\u3002\u521b\u5efa\u4e00\u4e2a\u91cd\u590d\u7f16\u53f7&#xff0c;\u80fd\u8ba9\u95ee\u9898\u5728\u6570\u636e\u5e93\u4e4b\u5916\u590d\u73b0\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u8ba4\u51b2\u7a81\u53d1\u751f\u5728\u96c6\u5408\u8f6c\u6362&#xff0c;\u4e0d\u5fc5\u540c\u65f6\u6392\u67e5\u7f51\u7edc\u548c\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n<p><span class=\"token keyword\">record<\/span> <span class=\"token class-name\">Device<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">String<\/span> code<span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">String<\/span> name<span class=\"token punctuation\">)<\/span> <span class=\"token punctuation\">{<\/span><span class=\"token punctuation\">}<\/span><\/p>\n<p><span class=\"token class-name\">List<\/span><span class=\"token generics\"><span class=\"token punctuation\">&lt;<\/span><span class=\"token class-name\">Device<\/span><span class=\"token punctuation\">&gt;<\/span><\/span> devices <span class=\"token operator\">&#061;<\/span> <span class=\"token class-name\">List<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">of<\/span><span class=\"token punctuation\">(<\/span><br \/>\n        <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">Device<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;E07&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;\u6e29\u5ea6\u8ba1&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n        <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">Device<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;E08&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;\u8ba1\u65f6\u5668&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n        <span class=\"token keyword\">new<\/span> <span class=\"token class-name\">Device<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">&#034;E07&#034;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#034;\u6e29\u5ea6\u8ba1\u5907\u7528\u540d&#034;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token class-name\">Map<\/span><span class=\"token generics\"><span class=\"token punctuation\">&lt;<\/span><span class=\"token class-name\">String<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">Device<\/span><span class=\"token punctuation\">&gt;<\/span><\/span> byCode <span class=\"token operator\">&#061;<\/span> devices<span class=\"token punctuation\">.<\/span><span class=\"token function\">stream<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">collect<\/span><span class=\"token punctuation\">(<\/span><br \/>\n        <span class=\"token class-name\">Collectors<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">toMap<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Device<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">code<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">Function<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">identity<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u9700\u8981\u5bfc\u5165 List\u3001Map\u3001Function \u4e0e Collectors\u3002\u9884\u671f\u8f6c\u6362\u65f6\u629b\u51fa IllegalStateException&#xff0c;\u5f02\u5e38\u6587\u672c\u4f1a\u5305\u542b\u952e\u51b2\u7a81\u4fe1\u606f&#xff1b;\u5177\u4f53\u6587\u6848\u4e0d\u5e94\u4f5c\u4e3a\u7a33\u5b9a\u63a5\u53e3\u5951\u7ea6\u3002\u672c\u7bc7\u672a\u6267\u884c\u8bfb\u8005\u9879\u76ee&#xff0c;\u8fd9\u91cc\u7ed9\u51fa\u4f9d\u636e JDK API \u7684\u9884\u671f\u884c\u4e3a\u3002<\/p>\n<p>\u67e5\u770b\u62a5\u9519\u952e\u540e&#xff0c;\u56de\u5230\u539f\u5217\u8868\u68c0\u67e5\u5bf9\u5e94\u8bb0\u5f55\u3002\u4e0d\u8981\u53ea\u770b\u7b2c\u4e00\u6761\u540d\u79f0\u76f8\u540c\u5c31\u8ba4\u5b9a\u5b83\u4eec\u5b8c\u5168\u7b49\u4ef7&#xff0c;\u8fd8\u8981\u6bd4\u8f83\u7248\u672c\u3001\u72b6\u6001\u4e0e\u4e1a\u52a1\u5f52\u5c5e\u3002\u751f\u4ea7\u65e5\u5fd7\u5e94\u53ea\u8bb0\u5f55\u5fc5\u8981\u6807\u8bc6\u7684\u8131\u654f\u503c&#xff0c;\u907f\u514d\u628a\u6574\u6761\u7528\u6237\u8bb0\u5f55\u6253\u5370\u51fa\u6765\u3002<\/p>\n<h3>\u4e09\u3001\u786e\u5b9e\u4e00\u5bf9\u591a\u65f6\u4f7f\u7528\u5206\u7ec4<\/h3>\n<p>\u5982\u679c\u4e00\u4e2a\u8bbe\u5907\u5bf9\u5e94\u591a\u6761\u501f\u7528\u8bb0\u5f55&#xff0c;\u6b63\u786e\u76ee\u6807\u5e94\u662f\u5217\u8868&#xff0c;\u800c\u4e0d\u662f\u4e00\u4e2a\u4efb\u610f\u4ee3\u8868\u503c\u3002\u53ef\u4ee5\u7528 groupingBy&#xff0c;\u628a\u6bcf\u4e2a\u7f16\u53f7\u4e0b\u7684\u5168\u90e8\u8bb0\u5f55\u4fdd\u7559\u4e0b\u6765&#xff1a;<\/p>\n<p><span class=\"token class-name\">Map<\/span><span class=\"token generics\"><span class=\"token punctuation\">&lt;<\/span><span class=\"token class-name\">String<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">List<\/span><span class=\"token punctuation\">&lt;<\/span><span class=\"token class-name\">Device<\/span><span class=\"token punctuation\">&gt;<\/span><span class=\"token punctuation\">&gt;<\/span><\/span> grouped <span class=\"token operator\">&#061;<\/span> devices<span class=\"token punctuation\">.<\/span><span class=\"token function\">stream<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">collect<\/span><span class=\"token punctuation\">(<\/span><br \/>\n        <span class=\"token class-name\">Collectors<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">groupingBy<\/span><span class=\"token punctuation\">(<\/span><span class=\"token class-name\">Device<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">code<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u8c03\u7528\u65b9\u968f\u540e\u6309\u8bbe\u5907\u7f16\u53f7\u8bfb\u53d6\u5217\u8868&#xff0c;\u5e76\u660e\u786e\u5904\u7406\u96f6\u6761\u3001\u4e00\u6761\u4e0e\u591a\u6761\u3002\u4e0d\u8981\u628a\u5206\u7ec4\u7ed3\u679c\u53c8\u76f4\u63a5\u53d6\u7b2c\u96f6\u9879&#xff0c;\u5426\u5219\u53ea\u662f\u628a\u4e22\u6570\u636e\u7684\u51b3\u5b9a\u79fb\u5230\u4e86\u4e0b\u4e00\u884c\u4ee3\u7801\u3002\u8fd4\u56de API \u65f6\u4e5f\u8981\u540c\u6b65\u7ed3\u679c\u6a21\u578b&#xff0c;\u524d\u7aef\u4e0d\u80fd\u4ecd\u6309\u5355\u4e2a\u5bf9\u8c61\u8bfb\u53d6\u3002<\/p>\n<p>\u82e5\u53ea\u662f\u7ed9\u73b0\u6709\u552f\u4e00\u5217\u8868\u5efa\u7acb\u7d22\u5f15&#xff0c;\u800c\u91cd\u590d\u6765\u81ea\u6570\u636e\u5e93 JOIN&#xff0c;\u4f18\u5148\u68c0\u67e5\u67e5\u8be2\u5173\u7cfb\u3002\u6bd4\u5982\u8bbe\u5907\u8054\u67e5\u9644\u4ef6\u65f6&#xff0c;\u4e00\u53f0\u8bbe\u5907\u6709\u4e24\u4e2a\u9644\u4ef6\u5c31\u4ea7\u751f\u4e24\u884c&#xff1b;\u6b64\u65f6\u53ef\u80fd\u9700\u8981\u5355\u72ec\u67e5\u8be2\u9644\u4ef6\u6216\u4f7f\u7528\u9002\u5408\u7684\u5bf9\u8c61\u6620\u5c04\u3002\u4f7f\u7528 DISTINCT \u662f\u5426\u6b63\u786e\u8981\u770b\u9009\u62e9\u5b57\u6bb5&#xff0c;\u4e0d\u80fd\u673a\u68b0\u6dfb\u52a0\u3002<\/p>\n<h3>\u56db\u3001\u4fdd\u7559\u9996\u6b21\u503c\u5fc5\u987b\u6709\u4e1a\u52a1\u6388\u6743<\/h3>\n<p>\u53ea\u6709\u4e1a\u52a1\u660e\u786e\u5141\u8bb8\u5ffd\u7565\u540e\u7eed\u540c\u952e\u8bb0\u5f55&#xff0c;\u624d\u4f7f\u7528\u5408\u5e76\u51fd\u6570\u3002\u4e0b\u9762\u6f14\u793a\u4fdd\u7559\u8f93\u5165\u4e2d\u7684\u9996\u6b21\u503c&#xff0c;\u5e76\u7528 LinkedHashMap \u4fdd\u6301\u952e\u9996\u6b21\u51fa\u73b0\u7684\u987a\u5e8f\u3002\u5b83\u662f\u4e00\u4e2a\u53ef\u9009\u7b56\u7565&#xff0c;\u4e0d\u662f\u901a\u7528\u9ed8\u8ba4\u4fee\u590d\u3002<\/p>\n<p><span class=\"token class-name\">Map<\/span><span class=\"token generics\"><span class=\"token punctuation\">&lt;<\/span><span class=\"token class-name\">String<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token class-name\">Device<\/span><span class=\"token punctuation\">&gt;<\/span><\/span> firstByCode <span class=\"token operator\">&#061;<\/span> devices<span class=\"token punctuation\">.<\/span><span class=\"token function\">stream<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">collect<\/span><span class=\"token punctuation\">(<\/span><br \/>\n        <span class=\"token class-name\">Collectors<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">toMap<\/span><span class=\"token punctuation\">(<\/span><br \/>\n                <span class=\"token class-name\">Device<\/span><span class=\"token operator\">::<\/span><span class=\"token function\">code<\/span><span class=\"token punctuation\">,<\/span><br \/>\n                <span class=\"token class-name\">Function<\/span><span class=\"token punctuation\">.<\/span><span class=\"token function\">identity<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n                <span class=\"token punctuation\">(<\/span>first<span class=\"token punctuation\">,<\/span> ignored<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">-&gt;<\/span> first<span class=\"token punctuation\">,<\/span><br \/>\n                <span class=\"token class-name\">LinkedHashMap<\/span><span class=\"token operator\">::<\/span><span class=\"token keyword\">new<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u5982\u679c\u5217\u8868\u6765\u81ea\u6ca1\u6709 ORDER BY \u7684\u6570\u636e\u5e93\u67e5\u8be2&#xff0c;\u201c\u9996\u6b21\u201d\u672c\u8eab\u53ef\u80fd\u4e0d\u7a33\u5b9a\u3002\u9700\u8981\u7a33\u5b9a\u7ed3\u679c\u65f6&#xff0c;\u5148\u5b9a\u4e49\u6392\u5e8f\u4f9d\u636e\u548c\u5e76\u5217\u88c1\u51b3&#xff0c;\u518d\u8f6c\u6362\u3002\u4fdd\u7559\u6700\u540e\u4e00\u6761\u4e5f\u9762\u4e34\u540c\u6837\u95ee\u9898&#xff0c;\u4e0d\u4ee3\u8868\u6700\u540e\u67e5\u5230\u7684\u4e00\u5b9a\u6700\u65b0\u3002<\/p>\n<p>\u9009\u62e9\u6700\u65b0\u7248\u672c\u65f6\u53ef\u6bd4\u8f83\u4e1a\u52a1\u7248\u672c\u53f7\u6216\u66f4\u65b0\u65f6\u95f4&#xff0c;\u4f46\u65f6\u95f4\u76f8\u540c\u4ecd\u9700\u89c4\u5219\u3002\u4e0d\u8981\u7528\u540d\u79f0\u5b57\u5178\u5e8f\u5192\u5145\u6700\u65b0&#xff0c;\u4e5f\u4e0d\u8981\u5728\u5408\u5e76\u51fd\u6570\u91cc\u987a\u624b\u8c03\u7528\u5916\u90e8\u63a5\u53e3\u6216\u4fee\u6539\u5171\u4eab\u72b6\u6001\u3002\u5408\u5e76\u903b\u8f91\u8d8a\u5c0f\u3001\u8d8a\u786e\u5b9a&#xff0c;\u8d8a\u5bb9\u6613\u6d4b\u8bd5\u4e0e\u590d\u7528\u3002<\/p>\n<h3>\u4e94\u3001\u9a8c\u8bc1\u4fdd\u7559\u89c4\u5219\u6709\u6ca1\u6709\u4e22\u4fe1\u606f<\/h3>\n<p>\u5bf9\u552f\u4e00\u952e\u8f93\u5165&#xff0c;Map \u5927\u5c0f\u5e94\u7b49\u4e8e\u5217\u8868\u5927\u5c0f&#xff1b;\u5bf9\u91cd\u590d\u952e\u8f93\u5165&#xff0c;\u5206\u7ec4\u540e\u6240\u6709\u5217\u8868\u957f\u5ea6\u4e4b\u548c\u5e94\u7b49\u4e8e\u539f\u59cb\u6761\u6570&#xff1b;\u5bf9\u9996\u6b21\u4fdd\u7559\u7b56\u7565&#xff0c;E07 \u5e94\u660e\u786e\u5bf9\u5e94\u7b2c\u4e00\u6761&#xff0c;\u800c\u4e0d\u662f\u5907\u7528\u540d\u3002\u518d\u6253\u4e71\u8f93\u5165\u987a\u5e8f&#xff0c;\u89c2\u5bdf\u9996\u6b21\u7b56\u7565\u662f\u5426\u6309\u8bbe\u8ba1\u6539\u53d8&#xff0c;\u5e2e\u52a9\u56e2\u961f\u7406\u89e3\u5b83\u4f9d\u8d56\u987a\u5e8f\u3002<\/p>\n<p>\u8fd8\u8981\u9a8c\u8bc1\u7a7a\u5217\u8868\u3001\u7a7a\u7f16\u53f7\u4e0e\u7a7a\u8bb0\u5f55\u3002\u793a\u4f8b\u4f7f\u7528 List.of \u4e0d\u80fd\u5305\u542b null&#xff0c;\u4f46\u771f\u5b9e\u6570\u636e\u5e93\u6216\u63a5\u53e3\u7ed3\u679c\u4ecd\u53ef\u80fd\u5b58\u5728\u7a7a\u5b57\u6bb5&#xff0c;\u5e94\u5728\u8fdb\u5165\u8f6c\u6362\u524d\u6309\u4e1a\u52a1\u89c4\u5219\u62d2\u7edd\u6216\u5355\u72ec\u5904\u7406\u3002\u4e0d\u540c Map \u5b9e\u73b0\u5bf9\u7a7a\u952e\u7684\u652f\u6301\u5e76\u4e0d\u4e00\u81f4&#xff0c;\u4e0d\u80fd\u4f9d\u8d56\u67d0\u6b21\u8fd0\u884c\u6ca1\u6709\u5f02\u5e38\u5c31\u5f53\u4f5c\u5951\u7ea6\u3002<\/p>\n<p>\u82e5\u5217\u8868\u975e\u5e38\u5927&#xff0c;\u5148\u8bc4\u4f30\u662f\u5426\u771f\u7684\u9700\u8981\u5185\u5b58\u7d22\u5f15\u6216\u5b8c\u6574\u5206\u7ec4\u3002\u5206\u9875\u67e5\u8be2\u5f97\u5230\u7684 Map \u53ea\u8986\u76d6\u5f53\u524d\u9875&#xff0c;\u4e0d\u80fd\u5f53\u4f5c\u5168\u91cf\u8bbe\u5907\u5b57\u5178\u4f7f\u7528\u3002\u82e5\u591a\u4e2a\u9875\u9762\u5171\u4eab\u7f13\u5b58&#xff0c;\u8fd8\u8981\u8bbe\u8ba1\u66f4\u65b0\u4e0e\u5931\u6548&#xff0c;\u4e0d\u8981\u628a\u8fd9\u4e2a\u5c40\u90e8\u8f6c\u6362\u51fd\u6570\u76f4\u63a5\u6269\u6210\u957f\u671f\u9759\u6001\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<h3>\u516d\u3001\u8ba9\u5f02\u5e38\u66b4\u9732\u771f\u5b9e\u6570\u636e\u95ee\u9898<\/h3>\n<p>\u5bf9\u4e8e\u672c\u5e94\u552f\u4e00\u7684\u6570\u636e&#xff0c;\u4fdd\u7559\u5f02\u5e38\u5f80\u5f80\u6709\u4ef7\u503c\u3002\u53ef\u4ee5\u5728\u670d\u52a1\u8fb9\u754c\u8fd4\u56de\u7a33\u5b9a\u7684\u5185\u90e8\u9519\u8bef\u7801&#xff0c;\u5e76\u8bb0\u5f55\u91cd\u590d\u6765\u6e90\u4f9b\u6392\u67e5&#xff0c;\u800c\u4e0d\u662f\u8ba9\u5b8c\u6574\u5806\u6808\u6cc4\u6f0f\u7ed9\u524d\u7aef\u3002\u4fee\u590d\u6e90\u6570\u636e\u65f6\u6309\u9879\u76ee\u7684\u6570\u636e\u8fc1\u79fb\u6d41\u7a0b\u6267\u884c&#xff0c;\u4e0d\u80fd\u5728\u6355\u83b7\u5f02\u5e38\u540e\u81ea\u52a8\u5220\u9664\u91cd\u590d\u8bb0\u5f55\u3002<\/p>\n<p>\u56e2\u961f\u5e94\u5728\u63a5\u53e3\u6587\u6863\u91cc\u6807\u660e\u952e\u7684\u552f\u4e00\u8303\u56f4&#xff1a;\u662f\u5168\u5c40\u7f16\u53f7&#xff0c;\u8fd8\u662f\u7ec4\u7ec7\u52a0\u7f16\u53f7\u7684\u7ec4\u5408&#xff1f;\u82e5\u4e0d\u540c\u5b9e\u9a8c\u5ba4\u5141\u8bb8\u76f8\u540c\u8bbe\u5907\u7f16\u53f7&#xff0c;\u5e94\u4f7f\u7528\u771f\u6b63\u7684\u552f\u4e00\u6807\u8bc6\u6216\u7ec4\u5408\u952e\u3002\u7b80\u5355\u62fc\u63a5\u5b57\u7b26\u4e32\u65f6\u8fd8\u8981\u907f\u514d\u5206\u9694\u7b26\u78b0\u649e&#xff0c;\u66f4\u7a33\u59a5\u7684\u662f\u5b9a\u4e49\u4e0d\u53ef\u53d8\u7ec4\u5408\u952e\u5bf9\u8c61\u3002<\/p>\n<p>\u5b8c\u6210\u9a8c\u6536\u540e&#xff0c;\u6d4b\u8bd5\u4e0d\u53ea\u65ad\u8a00\u201c\u4e0d\u629b\u5f02\u5e38\u201d&#xff0c;\u800c\u5e94\u65ad\u8a00\u7ed3\u679c\u5927\u5c0f\u3001\u6bcf\u4e2a\u952e\u7684\u5185\u5bb9\u548c\u88ab\u4fdd\u7559\u7684\u8bb0\u5f55\u3002\u628a\u5173\u7cfb\u3001\u5bb9\u5668\u548c\u5408\u5e76\u7b56\u7565\u5bf9\u5e94\u8d77\u6765&#xff0c;Duplicate key \u5c31\u80fd\u5f15\u5bfc\u4f60\u4fee\u6b63\u6570\u636e\u6a21\u578b&#xff0c;\u800c\u4e0d\u662f\u52a0\u5165\u4e00\u6bb5\u65e0\u6cd5\u89e3\u91ca\u7684\u53bb\u91cd\u4ee3\u7801\u3002<\/p>\n<p>\u5408\u5e76\u524d\u540e\u53ef\u4ee5\u8bb0\u5f55\u8f93\u5165\u6761\u6570\u3001\u4e0d\u540c\u952e\u6570\u91cf\u548c\u51b2\u7a81\u952e\u6570\u91cf&#xff0c;\u4f5c\u4e3a\u4e0d\u542b\u4e1a\u52a1\u6b63\u6587\u7684\u8bca\u65ad\u6307\u6807\u3002\u82e5\u5347\u7ea7\u67e5\u8be2\u540e\u51b2\u7a81\u7a81\u7136\u589e\u591a&#xff0c;\u5e94\u5148\u68c0\u67e5\u5173\u8054\u6761\u4ef6\u548c\u552f\u4e00\u8303\u56f4&#xff0c;\u800c\u4e0d\u662f\u7acb\u5373\u66f4\u6362\u5408\u5e76\u51fd\u6570\u3002\u5bf9\u4e8e\u9700\u8981\u62d2\u7edd\u91cd\u590d\u7684\u63a5\u53e3&#xff0c;\u6d4b\u8bd5\u5e94\u4fdd\u7559\u8fd9\u4e2a\u5f02\u5e38\u5206\u652f&#xff0c;\u9632\u6b62\u540e\u7eed\u7ef4\u62a4\u8005\u628a\u5b83\u8bef\u5f53\u6210\u53ef\u4ee5\u968f\u610f\u5ffd\u7565\u7684\u566a\u58f0\u3002<\/p>\n<h3>\u53c2\u8003\u8d44\u6599<\/h3>\n<ul>\n<li>Oracle Java 17&#xff1a;Collectors&#xff0c;toMap \u4e0e groupingBy\u3002<\/li>\n<li>Oracle Java 17&#xff1a;Map&#xff0c;\u952e\u4e0e\u503c\u7ea6\u675f&#xff1b;2026-10-31 \u6838\u9a8c\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u628a\u67e5\u8be2\u5217\u8868\u8f6c\u6362\u6210 Map \u540e&#xff0c;Java \u629b\u51fa Duplicate key&#xff0c;\u63a5\u53e3\u76f4\u63a5\u53d8\u6210 500\u3002\u7ed9 toMap \u968f\u624b\u52a0\u4e00\u4e2a\u4fdd\u7559\u7b2c\u4e00\u6761\u7684\u5408\u5e76\u51fd\u6570&#xff0c;\u867d\u7136\u53ef\u80fd\u4e0d\u518d\u62a5\u9519&#xff0c;\u5374\u4e5f\u53ef\u80fd\u4e22\u6389\u6709\u6548\u8bb0\u5f55\u3002\u672c\u7bc7\u4ee5 JDK 17 \u4e3a\u8303\u56f4&#xff0c;\u7528\u8bbe\u5907\u67e5\u8be2\u7ed3\u679c\u8bf4\u660e\u952e\u51b2\u7a81\u7684\u542b\u4e49&#xff0c;\u6309\u4e00\u5bf9\u4e00\u4e0e\u4e00\u5bf9\u591a\u5173\u7cfb\u9009\u62e9\u4fee\u590d&#xff0c;\u5e76\u7ed9\u51fa\u53ef\u533a\u5206\u6570\u636e\u7b56\u7565\u7684\u9a8c\u8bc1\u6837\u672c\u3002<br \/>\n\u4e00\u3001\u5148\u786e\u8ba4\u8fd9\u4e2a\u952e\u771f\u7684\u552f\u4e00\u5417<br \/>\nMap \u4e2d\u4e00\u4e2a\u952e\u53ea\u5bf9\u5e94\u4e00\u4e2a\u503c\u3002Colle<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[13886,9098,13343,99,4233,1838],"topic":[],"class_list":["post-121378","post","type-post","status-publish","format-standard","hentry","category-server","tag-collectors","tag-it","tag-stream","tag-java","tag-map","tag-1838"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Java Stream \u8f6c Map \u62a5 Duplicate key\uff1a\u660e\u786e\u91cd\u590d\u952e\u7b56\u7565 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/121378.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Stream \u8f6c Map \u62a5 Duplicate key\uff1a\u660e\u786e\u91cd\u590d\u952e\u7b56\u7565 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u628a\u67e5\u8be2\u5217\u8868\u8f6c\u6362\u6210 Map \u540e&#xff0c;Java \u629b\u51fa Duplicate key&#xff0c;\u63a5\u53e3\u76f4\u63a5\u53d8\u6210 500\u3002\u7ed9 toMap \u968f\u624b\u52a0\u4e00\u4e2a\u4fdd\u7559\u7b2c\u4e00\u6761\u7684\u5408\u5e76\u51fd\u6570&#xff0c;\u867d\u7136\u53ef\u80fd\u4e0d\u518d\u62a5\u9519&#xff0c;\u5374\u4e5f\u53ef\u80fd\u4e22\u6389\u6709\u6548\u8bb0\u5f55\u3002\u672c\u7bc7\u4ee5 JDK 17 \u4e3a\u8303\u56f4&#xff0c;\u7528\u8bbe\u5907\u67e5\u8be2\u7ed3\u679c\u8bf4\u660e\u952e\u51b2\u7a81\u7684\u542b\u4e49&#xff0c;\u6309\u4e00\u5bf9\u4e00\u4e0e\u4e00\u5bf9\u591a\u5173\u7cfb\u9009\u62e9\u4fee\u590d&#xff0c;\u5e76\u7ed9\u51fa\u53ef\u533a\u5206\u6570\u636e\u7b56\u7565\u7684\u9a8c\u8bc1\u6837\u672c\u3002 \u4e00\u3001\u5148\u786e\u8ba4\u8fd9\u4e2a\u952e\u771f\u7684\u552f\u4e00\u5417 Map \u4e2d\u4e00\u4e2a\u952e\u53ea\u5bf9\u5e94\u4e00\u4e2a\u503c\u3002Colle\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/121378.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-11-02T08:32:43+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/121378.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/121378.html\",\"name\":\"Java Stream \u8f6c Map \u62a5 Duplicate key\uff1a\u660e\u786e\u91cd\u590d\u952e\u7b56\u7565 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-11-02T08:32:43+00:00\",\"dateModified\":\"2026-11-02T08:32:43+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/121378.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/121378.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/121378.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java Stream \u8f6c Map \u62a5 Duplicate key\uff1a\u660e\u786e\u91cd\u590d\u952e\u7b56\u7565\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Stream \u8f6c Map \u62a5 Duplicate key\uff1a\u660e\u786e\u91cd\u590d\u952e\u7b56\u7565 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/121378.html","og_locale":"zh_CN","og_type":"article","og_title":"Java Stream \u8f6c Map \u62a5 Duplicate key\uff1a\u660e\u786e\u91cd\u590d\u952e\u7b56\u7565 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u628a\u67e5\u8be2\u5217\u8868\u8f6c\u6362\u6210 Map \u540e&#xff0c;Java \u629b\u51fa Duplicate key&#xff0c;\u63a5\u53e3\u76f4\u63a5\u53d8\u6210 500\u3002\u7ed9 toMap \u968f\u624b\u52a0\u4e00\u4e2a\u4fdd\u7559\u7b2c\u4e00\u6761\u7684\u5408\u5e76\u51fd\u6570&#xff0c;\u867d\u7136\u53ef\u80fd\u4e0d\u518d\u62a5\u9519&#xff0c;\u5374\u4e5f\u53ef\u80fd\u4e22\u6389\u6709\u6548\u8bb0\u5f55\u3002\u672c\u7bc7\u4ee5 JDK 17 \u4e3a\u8303\u56f4&#xff0c;\u7528\u8bbe\u5907\u67e5\u8be2\u7ed3\u679c\u8bf4\u660e\u952e\u51b2\u7a81\u7684\u542b\u4e49&#xff0c;\u6309\u4e00\u5bf9\u4e00\u4e0e\u4e00\u5bf9\u591a\u5173\u7cfb\u9009\u62e9\u4fee\u590d&#xff0c;\u5e76\u7ed9\u51fa\u53ef\u533a\u5206\u6570\u636e\u7b56\u7565\u7684\u9a8c\u8bc1\u6837\u672c\u3002 \u4e00\u3001\u5148\u786e\u8ba4\u8fd9\u4e2a\u952e\u771f\u7684\u552f\u4e00\u5417 Map \u4e2d\u4e00\u4e2a\u952e\u53ea\u5bf9\u5e94\u4e00\u4e2a\u503c\u3002Colle","og_url":"https:\/\/www.wsisp.com\/helps\/121378.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-11-02T08:32:43+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/121378.html","url":"https:\/\/www.wsisp.com\/helps\/121378.html","name":"Java Stream \u8f6c Map \u62a5 Duplicate key\uff1a\u660e\u786e\u91cd\u590d\u952e\u7b56\u7565 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-11-02T08:32:43+00:00","dateModified":"2026-11-02T08:32:43+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/121378.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/121378.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/121378.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Java Stream \u8f6c Map \u62a5 Duplicate key\uff1a\u660e\u786e\u91cd\u590d\u952e\u7b56\u7565"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/121378","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=121378"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/121378\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=121378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=121378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=121378"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=121378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}