{"id":1585,"date":"2026-06-22T08:41:33","date_gmt":"2026-06-22T00:41:33","guid":{"rendered":"https:\/\/yunyanglib.cn\/?p=1585"},"modified":"2026-06-22T08:41:35","modified_gmt":"2026-06-22T00:41:35","slug":"%e3%80%90java%e3%80%91%e9%9b%86%e5%90%88%e5%b7%a5%e5%85%b7%e7%b1%bb-collections-%e5%85%a8%e6%96%b9%e4%bd%8d%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/yunyanglib.cn\/?p=1585","title":{"rendered":"\u3010Java\u3011\u96c6\u5408\u5de5\u5177\u7c7b Collections \u5168\u65b9\u4f4d\u8be6\u89e3"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5927\u5bb6\u597d\uff0c\u6211\u662f\u4e91\u626c\uff0c\u4eca\u5929\u7ee7\u7eed\u66f4\u65b0 Java \u57fa\u7840\u5e72\u8d27\u5185\u5bb9\uff0c\u672c\u7bc7\u7cfb\u7edf\u68b3\u7406 JDK \u81ea\u5e26\u7684<code>java.util.Collections<\/code>\u5de5\u5177\u7c7b\u3002\u65e5\u5e38\u5f00\u53d1\u4e2d\u6211\u4eec\u7ecf\u5e38\u64cd\u4f5c List\u3001Set\u3001Map \u7b49\u96c6\u5408\uff0c\u9891\u7e41\u9700\u8981\u6392\u5e8f\u3001\u67e5\u627e\u3001\u7ebf\u7a0b\u5b89\u5168\u5904\u7406\u7b49\u64cd\u4f5c\uff0c\u624b\u52a8\u5b9e\u73b0\u7e41\u7410\u6613\u9519\uff0c\u800c<code>Collections<\/code>\u4f5c\u4e3a JDK \u539f\u751f\u63d0\u4f9b\u7684\u96c6\u5408\u5de5\u5177\uff0c\u5c01\u88c5\u4e86\u5927\u91cf\u9759\u6001\u65b9\u6cd5\uff0c\u662f\u5f00\u53d1\u9ad8\u9891\u5de5\u5177\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u524d\u7f6e\u5bfc\u5165<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Collections<\/code>\u5728<code>java.util<\/code>\u5305\u4e0b\uff0c\u4f7f\u7528\u524d\u5fc5\u987b\u5bfc\u5305\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Collections;\nimport java.util.ArrayList;\nimport java.util.List;\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u96c6\u5408\u6392\u5e8f\u76f8\u5173 API<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Collections<\/code>\u63d0\u4f9b\u591a\u6b3e\u9488\u5bf9 List \u7684\u6392\u5e8f\u3001\u8c03\u6362\u3001\u6253\u4e71\u65b9\u6cd5\uff0c\u65e5\u5e38\u6700\u5e38\u7528\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>reverse(List&lt;?> list)<\/code>\uff1a\u53cd\u8f6c\u96c6\u5408\u5143\u7d20\u987a\u5e8f<\/li>\n\n\n\n<li><code>shuffle(List&lt;?> list)<\/code>\uff1a\u968f\u673a\u6253\u4e71\u96c6\u5408\u987a\u5e8f\uff08\u62bd\u5956\u3001\u968f\u673a\u53d6\u503c\u5e38\u7528\uff09<\/li>\n\n\n\n<li><code>sort(List&lt;T> list)<\/code>\uff1a\u81ea\u7136\u5347\u5e8f\u6392\u5e8f\uff08\u5143\u7d20\u9700\u8981\u5b9e\u73b0 Comparable \u63a5\u53e3\uff09<\/li>\n\n\n\n<li><code>sort(List&lt;T> list, Comparator&lt;? super T> c)<\/code>\uff1a\u81ea\u5b9a\u4e49\u6bd4\u8f83\u5668\u6392\u5e8f<\/li>\n\n\n\n<li><code>swap(List&lt;?> list, int i, int j)<\/code>\uff1a\u4ea4\u6362\u4e0b\u6807 i\u3001j \u4f4d\u7f6e\u5143\u7d20<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class CollectionsSortDemo {\n    public static void main(String&#91;] args) {\n        List&lt;Integer&gt; list = new ArrayList&lt;&gt;();\n        Collections.addAll(list, 3, 1, 9, 5, 2);\n        System.out.println(\"\u539f\u59cb\u96c6\u5408\uff1a\" + list);\n\n        \/\/ \u81ea\u7136\u6392\u5e8f\n        Collections.sort(list);\n        System.out.println(\"\u5347\u5e8f\u6392\u5e8f\uff1a\" + list);\n\n        \/\/ \u53cd\u8f6c\n        Collections.reverse(list);\n        System.out.println(\"\u53cd\u8f6c\u540e\uff1a\" + list);\n\n        \/\/ \u968f\u673a\u6253\u4e71\n        Collections.shuffle(list);\n        System.out.println(\"\u968f\u673a\u6253\u4e71\uff1a\" + list);\n\n        \/\/ \u4ea4\u6362\u4e0b\u68070\u548c2\u5143\u7d20\n        Collections.swap(list,0,2);\n        System.out.println(\"\u4ea4\u6362\u4e0b\u68070\u30012\uff1a\" + list);\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u67e5\u627e\u4e0e\u586b\u5145\u7edf\u8ba1 API<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5305\u542b\u4e8c\u5206\u67e5\u627e\u3001\u6700\u503c\u83b7\u53d6\u3001\u5143\u7d20\u586b\u5145\u3001\u7edf\u8ba1\u5143\u7d20\u51fa\u73b0\u6b21\u6570\u7b49\u529f\u80fd\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>binarySearch(List&lt;? extends Comparable&lt;? super T>> list, T key)<\/code>\uff1a\u4e8c\u5206\u67e5\u627e\uff0c<strong>\u524d\u63d0\uff1a\u96c6\u5408\u5fc5\u987b\u6709\u5e8f<\/strong><\/li>\n\n\n\n<li><code>max\/min(Collection coll)<\/code>\uff1a\u83b7\u53d6\u96c6\u5408\u6700\u5927\u3001\u6700\u5c0f\u503c\uff08\u81ea\u7136\u6bd4\u8f83\uff09<\/li>\n\n\n\n<li><code>max\/min(Collection coll, Comparator comp)<\/code>\uff1a\u81ea\u5b9a\u4e49\u89c4\u5219\u83b7\u53d6\u6700\u503c<\/li>\n\n\n\n<li><code>fill(List&lt;? super T> list, T obj)<\/code>\uff1a\u4f7f\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\u8986\u76d6\u96c6\u5408\u6240\u6709\u5143\u7d20<\/li>\n\n\n\n<li><code>frequency(Collection&lt;?> c, Object o)<\/code>\uff1a\u7edf\u8ba1\u6307\u5b9a\u5143\u7d20\u5728\u96c6\u5408\u4e2d\u51fa\u73b0\u6b21\u6570<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class CollectionsSearchDemo {\n    public static void main(String&#91;] args) {\n        List&lt;String&gt; list = new ArrayList&lt;&gt;();\n        Collections.addAll(list,\"\u5f20\u4e09\",\"\u674e\u56db\",\"\u5f20\u4e09\",\"\u738b\u4e94\");\n\n        \/\/ \u7edf\u8ba1\u5f20\u4e09\u51fa\u73b0\u6b21\u6570\n        int count = Collections.frequency(list,\"\u5f20\u4e09\");\n        System.out.println(\"\u5f20\u4e09\u51fa\u73b0\u6b21\u6570\uff1a\"+count);\n\n        \/\/ \u586b\u5145\u5168\u90e8\u5143\u7d20\u4e3a\u3010\u6d4b\u8bd5\u3011\n        Collections.fill(list,\"\u6d4b\u8bd5\");\n        System.out.println(\"fill\u586b\u5145\u540e\uff1a\"+list);\n\n        List&lt;Integer&gt; numList = new ArrayList&lt;&gt;();\n        Collections.addAll(numList,12,5,89,22);\n        \/\/ \u83b7\u53d6\u6700\u5927\u503c\n        Integer max = Collections.max(numList);\n        System.out.println(\"\u6700\u5927\u503c\uff1a\"+max);\n\n        \/\/ \u4e8c\u5206\u67e5\u627e\u524d\u5fc5\u987b\u6392\u5e8f\n        Collections.sort(numList);\n        int index = Collections.binarySearch(numList,22);\n        System.out.println(\"22\u7684\u4e0b\u6807\uff1a\"+index);\n    }\n}\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u6ce8\u610f\uff1a<code>binarySearch<\/code>\u65e0\u5e8f\u96c6\u5408\u67e5\u8be2\u7ed3\u679c\u4e0d\u53ef\u9760\uff0c\u627e\u4e0d\u5230\u5143\u7d20\u4f1a\u8fd4\u56de\u8d1f\u6570\u3002<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u591a\u7ebf\u7a0b\uff1a\u540c\u6b65\u5b89\u5168\u96c6\u5408<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u539f\u751f ArrayList\u3001HashMap \u662f\u975e\u7ebf\u7a0b\u5b89\u5168\u96c6\u5408\uff0c\u591a\u7ebf\u7a0b\u5e76\u53d1\u8bfb\u5199\u4f1a\u51fa\u73b0\u5e76\u53d1\u5f02\u5e38\u3002<code>Collections.synchronizedXxx()<\/code>\u7cfb\u5217\u65b9\u6cd5\u53ef\u4ee5\u628a\u666e\u901a\u96c6\u5408\u5305\u88c5\u6210\u540c\u6b65\u96c6\u5408\uff0c\u5e95\u5c42\u901a\u8fc7<code>synchronized<\/code>\u52a0\u9501\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>synchronizedList(List&lt;T> list)<\/code><\/li>\n\n\n\n<li><code>synchronizedSet(Set&lt;T> s)<\/code><\/li>\n\n\n\n<li><code>synchronizedMap(Map&lt;K,V> m)<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u5c06\u666e\u901aArrayList\u8f6c\u4e3a\u7ebf\u7a0b\u5b89\u5168List\nList&lt;String&gt; safeList = Collections.synchronizedList(new ArrayList&lt;&gt;());\n<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u539f\u7406\uff1a\u5305\u88c5\u7c7b\u4e2d\u6240\u6709\u96c6\u5408\u64cd\u4f5c\u65b9\u6cd5\u90fd\u88ab<code>synchronized<\/code>\u4fee\u9970\uff0c\u540c\u4e00\u65f6\u95f4\u4ec5\u4e00\u4e2a\u7ebf\u7a0b\u80fd\u64cd\u4f5c\u96c6\u5408\uff0c\u727a\u7272\u90e8\u5206\u6027\u80fd\u6362\u53d6\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u521b\u5efa\u4e0d\u53ef\u53d8\u96c6\u5408<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u53ef\u53d8\u96c6\u5408\u521d\u59cb\u5316\u540e<strong>\u4e0d\u80fd\u65b0\u589e\u3001\u5220\u9664\u3001\u4fee\u6539\u5143\u7d20<\/strong>\uff0c\u4fee\u6539\u76f4\u63a5\u629b\u51fa<code>UnsupportedOperationException<\/code>\uff0c\u5e38\u7528\u4e8e\u5e38\u91cf\u6570\u636e\u5b58\u50a8\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><code>emptyList()\/emptySet()\/emptyMap()<\/code>\uff1a\u521b\u5efa\u7a7a\u4e0d\u53ef\u53d8\u96c6\u5408<\/li>\n\n\n\n<li><code>singletonList(T o)\/singletonSet(T o)<\/code>\uff1a\u4ec5\u5b58\u5355\u4e2a\u5143\u7d20\u7684\u4e0d\u53ef\u53d8\u96c6\u5408<\/li>\n\n\n\n<li><code>unmodifiableList(List&lt;? extends T> list)<\/code>\uff1a\u57fa\u4e8e\u539f\u6709\u96c6\u5408\u751f\u6210\u4e0d\u53ef\u53d8\u89c6\u56fe<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4ee3\u7801\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class UnModifiableDemo {\n    public static void main(String&#91;] args) {\n        \/\/ \u5355\u4e2a\u5143\u7d20\u4e0d\u53ef\u53d8\u96c6\u5408\n        List&lt;String&gt; singleList = Collections.singletonList(\"\u56fa\u5b9a\u53c2\u6570\");\n        \/\/ singleList.add(\"\u65b0\u589e\"); \/\/ \u62a5\u9519\uff1a\u4e0d\u652f\u6301\u4fee\u6539\u64cd\u4f5c\n\n        List&lt;Integer&gt; src = new ArrayList&lt;&gt;();\n        src.add(1);\n        List&lt;Integer&gt; unModiList = Collections.unmodifiableList(src);\n        \/\/ unModiList.add(2); \/\/ \u8fd0\u884c\u5f02\u5e38\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u5176\u4ed6\u5b9e\u7528\u65b9\u6cd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. addAll \u6279\u91cf\u6dfb\u52a0\u5143\u7d20<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5feb\u901f\u5f80\u96c6\u5408\u6279\u91cf\u63d2\u5165\u53ef\u53d8\u53c2\u6570\u5143\u7d20\uff0c\u66ff\u4ee3\u591a\u6b21 add \u8c03\u7528\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;Integer&gt; list = new ArrayList&lt;&gt;();\nCollections.addAll(list,10,20,30,40);\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. disjoint(Collection c1,Collection c2)<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5224\u65ad\u4e24\u4e2a\u96c6\u5408<strong>\u662f\u5426\u65e0\u4ea4\u96c6<\/strong>\uff0c\u65e0\u4ea4\u96c6\u8fd4\u56de true\uff0c\u5b58\u5728\u76f8\u540c\u5143\u7d20\u8fd4\u56de false\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>List&lt;Integer&gt; l1 = List.of(1,2,3);\nList&lt;Integer&gt; l2 = List.of(4,5,6);\nboolean isNoJoin = Collections.disjoint(l1,l2);\nSystem.out.println(\"\u662f\u5426\u65e0\u4ea4\u96c6\uff1a\"+isNoJoin);\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e03\u3001\u62d3\u5c55\uff1a\u7b2c\u4e09\u65b9\u96c6\u5408\u5de5\u5177\u7c7b<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">JDK \u539f\u751f<code>Collections<\/code>\u529f\u80fd\u6709\u9650\uff0c\u9879\u76ee\u5f00\u53d1\u4e2d\u5e38\u7528<strong>Apache Commons Collections<\/strong>\u3001<strong>Spring CollectionUtils<\/strong>\u4e24\u4e2a\u7b2c\u4e09\u65b9\u5de5\u5177\uff0c\u8865\u5145\u4ea4\u96c6\u3001\u5e76\u96c6\u3001\u5dee\u96c6\u3001\u96c6\u5408\u5224\u7a7a\u7b49\u80fd\u529b\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u4f9d\u8d56\u5f15\u5165 Maven \u5750\u6807\uff08Apache\uff09<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;dependency&gt;\n    &lt;groupId&gt;org.apache.commons&lt;\/groupId&gt;\n    &lt;artifactId&gt;commons-collections4&lt;\/artifactId&gt;\n    &lt;version&gt;4.4&lt;\/version&gt;\n&lt;\/dependency&gt;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5e38\u7528\u65b9\u6cd5\u793a\u4f8b\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.apache.commons.collections4.CollectionUtils;\nimport java.util.List;\npublic class ThirdCollectionDemo {\n    public static void main(String&#91;] args) {\n        List&lt;Integer&gt; list1 = List.of(1,2,3,4);\n        List&lt;Integer&gt; list2 = List.of(3,4,5,6);\n\n        \/\/ \u5e76\u96c6\n        CollectionUtils.union(list1,list2);\n        \/\/ \u4ea4\u96c6\n        CollectionUtils.intersection(list1,list2);\n        \/\/ \u5dee\u96c6 list1\u72ec\u6709\n        CollectionUtils.subtract(list1,list2);\n        \/\/ \u5bf9\u79f0\u5dee\u96c6\uff08\u4ea4\u96c6\u4ee5\u5916\u6240\u6709\u5143\u7d20\uff09\n        CollectionUtils.disjunction(list1,list2);\n    }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Spring \u7684<code>org.springframework.util.CollectionUtils<\/code>\u66f4\u591a\u7528\u4e8e\u96c6\u5408\u5224\u7a7a\uff1a<code>CollectionUtils.isEmpty(\u96c6\u5408)<\/code>\uff0c\u65e5\u5e38\u5f00\u53d1\u5224\u7a7a\u9996\u9009\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7ed3\u8bed<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Collections<\/code>\u662f Java \u96c6\u5408\u4f53\u7cfb\u5fc5\u638c\u63e1\u57fa\u7840\u5de5\u5177\uff0c\u719f\u7ec3\u4f7f\u7528\u53ef\u4ee5\u5927\u5e45\u7b80\u5316\u96c6\u5408\u4ee3\u7801\uff0c\u51cf\u5c11\u91cd\u590d\u9020\u8f6e\u5b50\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5927\u5bb6\u597d\uff0c\u6211\u662f\u4e91\u626c\uff0c\u4eca\u5929\u7ee7\u7eed\u66f4\u65b0 Java \u57fa\u7840\u5e72\u8d27\u5185\u5bb9\uff0c\u672c\u7bc7\u7cfb\u7edf\u68b3\u7406 JDK \u81ea\u5e26\u7684java.util.Col<\/p>\n<div class=\"more-link\">\n\t\t\t\t <a href=\"https:\/\/yunyanglib.cn\/?p=1585\" class=\"link-btn theme-btn\"><span>Read More <\/span> <i class=\"fa fa-caret-right\"><\/i><\/a>\n\t\t\t<\/div>\n","protected":false},"author":1,"featured_media":1586,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[15],"class_list":["post-1585","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","tag-java"],"_links":{"self":[{"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=\/wp\/v2\/posts\/1585","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1585"}],"version-history":[{"count":2,"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=\/wp\/v2\/posts\/1585\/revisions"}],"predecessor-version":[{"id":1588,"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=\/wp\/v2\/posts\/1585\/revisions\/1588"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=\/wp\/v2\/media\/1586"}],"wp:attachment":[{"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yunyanglib.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}