{"id":303269,"date":"2024-05-20T18:55:56","date_gmt":"2024-05-20T10:55:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/303269.html"},"modified":"2024-05-20T18:56:26","modified_gmt":"2024-05-20T10:56:26","slug":"collection-%e5%92%8c-collections-%e6%9c%89%e4%bb%80%e4%b9%88%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/303269.html","title":{"rendered":"Collection \u548c Collections \u6709\u4ec0\u4e48\u533a\u522b"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26215534\/80d3b481-35c5-4647-8e6f-40cfeba6fe0d.webp\" alt=\"Collection \u548c Collections \u6709\u4ec0\u4e48\u533a\u522b\" \/><\/p>\n<p><p><strong>Collection\u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u800cCollections\u662f\u4e00\u4e2a\u5305\u542b\u9759\u6001\u65b9\u6cd5\u7684\u5de5\u5177\u7c7b\u3002Collection\u662f\u96c6\u5408\u6846\u67b6\u7684\u6839\u63a5\u53e3\uff0c\u5b9a\u4e49\u4e86\u5355\u5217\u6570\u636e\u7684\u901a\u7528\u64cd\u4f5c\uff0c\u5982\u6dfb\u52a0\u3001\u5220\u9664\u548c\u8fed\u4ee3\u5143\u7d20\u3002\u800cCollections\u662fjava.util\u5305\u4e2d\u7684\u4e00\u4e2a\u7c7b\uff0c\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u9759\u6001\u65b9\u6cd5\u6765\u64cd\u4f5c\u6216\u8fd4\u56de\u96c6\u5408\uff0c\u4f8b\u5982\u6392\u5e8f\u3001\u641c\u7d22\u548c\u7ebf\u7a0b\u5b89\u5168\u7684\u8f6c\u6362\u7b49\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>Collection\u63a5\u53e3<\/strong> \u662fJava\u96c6\u5408\u6846\u67b6\u7684\u6838\u5fc3\u63a5\u53e3\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u7ec4\u6807\u51c6\u7684\u65b9\u6cd5\u6765\u64cd\u4f5c\u5404\u79cd\u7c7b\u578b\u7684\u96c6\u5408\u5143\u7d20\u3002\u5b83\u88ab\u4e00\u4e9b\u96c6\u5408\u7c7b\u5b9e\u73b0\uff0c\u6bd4\u5982ArrayList\u3001LinkedList\u548cHashSet\u7b49\u3002\u8fd9\u4e9b\u5b9e\u73b0\u63d0\u4f9b\u4e86\u96c6\u5408\u7684\u57fa\u672c\u64cd\u4f5c\uff0c\u4f8b\u5982\u6dfb\u52a0\u3001\u5220\u9664\u3001\u6e05\u7a7a\u3001\u4ee5\u53ca\u8bbf\u95ee\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u3002Collection\u63a5\u53e3\u7684\u4f5c\u7528\u662f\u7edf\u4e00\u4e86\u5bf9\u96c6\u5408\u7684\u5904\u7406\u65b9\u5f0f\uff0c\u8ba9\u6211\u4eec\u53ef\u4ee5\u5c06\u4e0d\u540c\u7c7b\u578b\u7684\u96c6\u5408\u5f53\u4f5c\u7c7b\u4f3c\u7684\u5bf9\u8c61\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p><strong>Collections\u5de5\u5177\u7c7b<\/strong> \u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u9759\u6001\u65b9\u6cd5\uff0c\u7528\u4e8e\u8f85\u52a9\u6211\u4eec\u8fdb\u884c\u96c6\u5408\u7684\u64cd\u4f5c\uff0c\u6bd4\u5982\u6392\u5e8f\uff08sort\uff09\u3001\u67e5\u627e\uff08binarySearch\uff09\u3001\u7ebf\u7a0b\u5b89\u5168\u8f6c\u6362\uff08synchronizedCollection\uff09\u7b49\u3002\u8fd9\u4e9b\u5de5\u5177\u65b9\u6cd5\u5927\u591a\u662f\u9488\u5bf9Collection\u5b9e\u73b0\u7c7b\u6216\u8005\u5176\u5b50\u63a5\u53e3\u5982List\u6216Set\u7684\u64cd\u4f5c\u8fdb\u884c\u4f18\u5316\u7684\u3002\u4f7f\u7528Collections\u5de5\u5177\u7c7b\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u7f16\u7a0b\u7684\u590d\u6742\u5ea6\uff0c\u5e76\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7a33\u5b9a\u6027\u3002<\/p>\n<\/p>\n<p><p>\u5728Java\u7f16\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u7528\u5230\u8fd9\u4e24\u8005\u3002\u9009\u62e9Collection\u63a5\u53e3\u6216\u5176\u5b9e\u73b0\u7c7b\u662f\u4e3a\u4e86\u5b9a\u4e49\u548c\u4f7f\u7528\u5177\u4f53\u7684\u96c6\u5408\uff0c\u800c\u5728\u5bf9\u96c6\u5408\u672c\u8eab\u8fdb\u884c\u64cd\u4f5c\u65f6\uff08\u5982\u6392\u5e8f\u6216\u67e5\u627e\uff09\uff0c\u5219\u4f1a\u4f7f\u7528\u5230Collections\u5de5\u5177\u7c7b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001COLLECTION \u63a5\u53e3<\/h3>\n<\/p>\n<p><h4>\u5e38\u7528\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>Collection\u63a5\u53e3\u5b9a\u4e49\u4e86\u4e00\u4e9b\u57fa\u7840\u7684\u65b9\u6cd5\uff0c\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>boolean add(E e)<\/code>: \u6dfb\u52a0\u5143\u7d20\u5230\u96c6\u5408\u3002<\/li>\n<li><code>boolean remove(Object o)<\/code>: \u4ece\u96c6\u5408\u4e2d\u79fb\u9664\u6307\u5b9a\u5143\u7d20\u3002<\/li>\n<li><code>void clear()<\/code>: \u6e05\u7a7a\u96c6\u5408\u3002<\/li>\n<li><code>boolean cont<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ns(Object o)<\/code>: \u5224\u65ad\u96c6\u5408\u662f\u5426\u5305\u542b\u6307\u5b9a\u5143\u7d20\u3002<\/li>\n<li><code>int size()<\/code>: \u83b7\u53d6\u96c6\u5408\u4e2d\u7684\u5143\u7d20\u6570\u91cf\u3002<\/li>\n<li><code>boolean isEmpty()<\/code>: \u5224\u65ad\u96c6\u5408\u662f\u5426\u4e3a\u7a7a\u3002<\/li>\n<li><code>Iterator&lt;E&gt; iterator()<\/code>: \u83b7\u53d6\u7528\u4e8e\u904d\u5386\u96c6\u5408\u7684\u8fed\u4ee3\u5668\u3002<\/li>\n<\/ul>\n<p><p>\u96c6\u5408\u7c7b\u5982ArrayList\u548cHashSet\u63d0\u4f9b\u4e86\u8fd9\u4e9b\u65b9\u6cd5\u7684\u5177\u4f53\u5b9e\u73b0\u3002\u8fd9\u610f\u5473\u7740\uff0c\u65e0\u8bba\u6211\u4eec\u4f7f\u7528\u54ea\u79cd\u7c7b\u578b\u7684\u96c6\u5408\uff0c\u6211\u4eec\u90fd\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u901a\u7528\u7684\u65b9\u6cd5\u6765\u64cd\u4f5c\u5b83\u4eec\u3002<\/p>\n<\/p>\n<p><h4>\u5b9e\u73b0\u7ec6\u8282<\/h4>\n<\/p>\n<p><p>\u6bcf\u4e00\u4e2a\u5177\u4f53\u7684\u96c6\u5408\u7c7b\uff0c\u6bd4\u5982ArrayList\u6216\u8005HashSet\uff0c\u90fd\u6709\u5b83\u81ea\u5df1\u7684\u5185\u90e8\u6570\u636e\u7ed3\u6784\u3002<strong>ArrayList<\/strong> \u4f7f\u7528\u53ef\u8c03\u6574\u5927\u5c0f\u7684\u6570\u7ec4\u7ed3\u6784\u6765\u5b58\u50a8\u5143\u7d20\uff0c\u4f7f\u5f97\u7d22\u5f15\u76f8\u5173\u7684\u64cd\u4f5c\u53d8\u5f97\u9ad8\u6548\uff1b\u800c<strong>HashSet<\/strong> \u4f7f\u7528\u54c8\u5e0c\u8868\u6765\u5b9e\u73b0\u5143\u7d20\u7684\u5b58\u50a8\uff0c\u63d0\u4f9b\u5feb\u901f\u7684\u67e5\u627e\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001COLLECTIONS \u7c7b\u7684\u9759\u6001\u65b9\u6cd5<\/h3>\n<\/p>\n<p><h4>\u6392\u5e8f\u548c\u641c\u7d22<\/h4>\n<\/p>\n<p><p>Collections\u7c7b\u63d0\u4f9b\u7684<strong>sort<\/strong>\u65b9\u6cd5\u53ef\u4ee5\u5bf9List\u7c7b\u578b\u7684\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\u3002\u8fd8\u6709<strong>binarySearch<\/strong>\u65b9\u6cd5\u53ef\u4ee5\u5728\u5df2\u6392\u5e8f\u7684List\u4e2d\u5feb\u901f\u641c\u7d22\u5143\u7d20\u3002<strong>\u8fd9\u4e9b\u65b9\u6cd5\u6781\u5927\u5730\u7b80\u5316\u4e86\u6392\u5e8f\u548c\u641c\u7d22\u7684\u64cd\u4f5c\u6d41\u7a0b<\/strong>\u3002<\/p>\n<\/p>\n<p><h4>\u7ebf\u7a0b\u5b89\u5168\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>Collections\u7c7b\u4e2d\u7684<strong>synchronizedCollection<\/strong>\u3001<strong>synchronizedList<\/strong>\u3001<strong>synchronizedSet<\/strong>\u7b49\u65b9\u6cd5\u63d0\u4f9b\u4e86\u5c06\u975e\u7ebf\u7a0b\u5b89\u5168\u7684\u96c6\u5408\u8f6c\u6362\u4e3a\u7ebf\u7a0b\u5b89\u5168\u7684\u96c6\u5408\u7684\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><h4>\u4e0d\u53ef\u4fee\u6539\u96c6\u5408<\/h4>\n<\/p>\n<p><p>Collections\u8fd8\u63d0\u4f9b\u4e86<strong>unmodifiableCollection<\/strong>\u3001<strong>unmodifiableList<\/strong>\u3001<strong>unmodifiableSet<\/strong>\u7b49\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5c06\u96c6\u5408\u5305\u88c5\u6210\u4e0d\u53ef\u4fee\u6539\u7684\u5f62\u6001\uff0c\u5c1d\u8bd5\u4fee\u6539\u8fd9\u4e9b\u96c6\u5408\u5c06\u629b\u51faUnsupportedOperationException\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001COLLECTION \u548c COLLECTIONS \u7684\u4f7f\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>\u96c6\u5408\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5f53\u4f60\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u96c6\u5408\u5e76\u5728\u5176\u4e0a\u6267\u884c\u64cd\u4f5c\u65f6\uff0c\u4f60\u4f1a\u9009\u62e9\u5b9e\u73b0\u4e86Collection\u63a5\u53e3\u7684\u5177\u4f53\u7c7b\u3002\u4f8b\u5982\uff0c\u5f53\u9700\u8981\u6309\u7167\u63d2\u5165\u987a\u5e8f\u4fdd\u5b58\u5143\u7d20\u65f6\uff0c\u4f60\u53ef\u80fd\u4f1a\u9009\u62e9ArrayList\u3002<\/p>\n<\/p>\n<p><h4>\u5de5\u5177\u64cd\u4f5c<\/h4>\n<\/p>\n<p><p>\u5f53\u4f60\u62e5\u6709\u4e00\u4e2a\u5df2\u7ecf\u521b\u5efa\u597d\u7684\u96c6\u5408\uff0c\u5e76\u4e14\u9700\u8981\u6267\u884c\u4e00\u4e9b\u5de5\u5177\u64cd\u4f5c\u5982\u6392\u5e8f\u6216\u641c\u7d22\u65f6\uff0c\u4f60\u4f1a\u8f6c\u5411\u4f7f\u7528Collections\u7c7b\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6df1\u5165\u7406\u89e3<\/h3>\n<\/p>\n<p><p><strong>\u4e3a\u4ec0\u4e48\u9700\u8981Collections\u5de5\u5177\u7c7b<\/strong>: Collection\u63a5\u53e3\u53ca\u5176\u5b9e\u73b0\u5173\u6ce8\u4e8e\u96c6\u5408\u672c\u8eab\u7684\u6570\u636e\u7ed3\u6784\u548c\u5143\u7d20\u64cd\u4f5c\u3002\u76f8\u5bf9\u5730\uff0cCollections\u7c7b\u5305\u542b\u7684\u9759\u6001\u65b9\u6cd5\u5219\u662f\u5bf9\u4e8e\u96c6\u5408\u5bf9\u8c61\u7684\u64cd\u4f5c\uff0c\u8fd9\u4e0d\u662f\u5355\u4e00\u96c6\u5408\u6240\u7279\u6709\u7684\uff0c\u66f4\u591a\u7684\u662f\u4e00\u4e9b\u516c\u7528\u7684\u5de5\u5177\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p><strong>\u5982\u4f55\u9009\u62e9<\/strong>: \u5bf9\u4e8e\u5177\u4f53\u7684\u4e1a\u52a1\u573a\u666f\uff0c\u6839\u636e\u9700\u8981\u7ba1\u7406\u7684\u6570\u636e\u7c7b\u578b\u548c\u64cd\u4f5c\u7684\u7279\u70b9\u6765\u9009\u62e9\u4f7f\u7528Collection\u7684\u5b9e\u73b0\u7c7b\u8fd8\u662fCollections\u7c7b\u7684\u9759\u6001\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7684\u6765\u8bf4\uff0cCollection\u548cCollections\u5206\u522b\u5728Java\u96c6\u5408\u6846\u67b6\u4e2d\u626e\u6f14\u7740\u521b\u5efa\u548c\u64cd\u4f5c\u96c6\u5408\u7684\u89d2\u8272\u3002\u638c\u63e1\u5b83\u4eec\u7684\u4f7f\u7528\u80fd\u8ba9\u96c6\u5408\u6570\u636e\u7684\u7ba1\u7406\u53d8\u5f97\u66f4\u52a0\u7075\u6d3b\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. Collection\u548cCollections\u6709\u4ec0\u4e48\u4e0d\u540c\uff1f<\/strong><br \/>\nCollection\u548cCollections\u5728Java\u4e2d\u90fd\u8868\u793a\u96c6\u5408\u7684\u6982\u5ff5\uff0c\u4f46\u4e24\u8005\u4e4b\u95f4\u5b58\u5728\u4e00\u4e9b\u533a\u522b\u3002Collection\u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u5b9a\u4e49\u4e86\u5404\u79cd\u96c6\u5408\u7c7b\u7684\u901a\u7528\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5982\u6dfb\u52a0\u3001\u5220\u9664\u3001\u68c0\u7d22\u5143\u7d20\u7b49\u3002\u800cCollections\u5219\u662f\u4e00\u4e2a\u5de5\u5177\u7c7b\uff0c\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u9759\u6001\u65b9\u6cd5\uff0c\u7528\u4e8e\u5bf9\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\u3001\u641c\u7d22\u3001\u590d\u5236\u7b49\u64cd\u4f5c\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u9009\u62e9\u4f7f\u7528Collection\u8fd8\u662fCollections\uff1f<\/strong><br \/>\n\u5f53\u4f60\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u96c6\u5408\u5bf9\u8c61\u5e76\u4f7f\u7528\u5176\u64cd\u4f5c\u65b9\u6cd5\u65f6\uff0c\u5e94\u8be5\u4f7f\u7528Collection\u63a5\u53e3\u3002\u4f60\u53ef\u4ee5\u9009\u62e9\u4f7f\u7528\u5176\u4e2d\u7684\u5b50\u63a5\u53e3\u6216\u5b9e\u73b0\u7c7b\uff0c\u5982List\u3001Set\u7b49\u3002\u800c\u5f53\u4f60\u9700\u8981\u5bf9\u73b0\u6709\u7684\u96c6\u5408\u8fdb\u884c\u4e00\u4e9b\u64cd\u4f5c\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7Collections\u5de5\u5177\u7c7b\u6765\u8fdb\u884c\u3002\u5b83\u63d0\u4f9b\u4e86\u5f88\u591a\u65b9\u4fbf\u7684\u65b9\u6cd5\uff0c\u5982\u5bf9\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\u3001\u67e5\u627e\u6700\u5927\/\u6700\u5c0f\u503c\u3001\u5012\u5e8f\u6392\u5217\u7b49\u3002<\/p>\n<p><strong>3. Collection\u548cCollections\u53ef\u4ee5\u540c\u65f6\u4f7f\u7528\u5417\uff1f<\/strong><br \/>\n\u662f\u7684\uff0c\u4f60\u53ef\u4ee5\u540c\u65f6\u4f7f\u7528Collection\u548cCollections\u3002\u4f8b\u5982\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Collection\u63a5\u53e3\u7684\u5b9e\u73b0\u7c7bArrayList\u521b\u5efa\u4e00\u4e2a\u96c6\u5408\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528Collections\u5de5\u5177\u7c7b\u5bf9\u8be5\u96c6\u5408\u8fdb\u884c\u6392\u5e8f\u6216\u67e5\u627e\u64cd\u4f5c\u3002\u8fd9\u6837\u7ed3\u5408\u4f7f\u7528Collection\u548cCollections\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5b8c\u6210\u5404\u79cd\u96c6\u5408\u64cd\u4f5c\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u6548\u7387\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Collection\u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u800cCollections\u662f\u4e00\u4e2a\u5305\u542b\u9759\u6001\u65b9\u6cd5\u7684\u5de5\u5177\u7c7b\u3002Collection\u662f\u96c6\u5408 [&hellip;]","protected":false},"author":3,"featured_media":303308,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/303269"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=303269"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/303269\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/303308"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=303269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=303269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=303269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}