{"id":94062,"date":"2026-08-13T16:16:45","date_gmt":"2026-08-13T08:16:45","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/94062.html"},"modified":"2026-08-13T16:16:45","modified_gmt":"2026-08-13T08:16:45","slug":"collections-%e5%b7%a5%e5%85%b7%e7%b1%bb%e5%b8%b8%e7%94%a8%e6%96%b9%e6%b3%95%e5%85%a8%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/94062.html","title":{"rendered":"Collections \u5de5\u5177\u7c7b\u5e38\u7528\u65b9\u6cd5\u5168\u89e3"},"content":{"rendered":"<\/p>\n<p>import java.util.Collections;<\/p>\n<p>\u2460 sort(List list)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u5bf9\u5217\u8868\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f&#xff08;\u81ea\u7136\u6392\u5e8f&#xff09;\u3002\u5217\u8868\u5143\u7d20\u5fc5\u987b\u5b9e\u73b0 Comparable \u63a5\u53e3\u3002<\/p>\n<p>List&lt;Integer&gt; list &#061; new ArrayList&lt;&gt;();<br \/>\nlist.add(3);<br \/>\nlist.add(1);<br \/>\nlist.add(4);<br \/>\nlist.add(2);<br \/>\nCollections.sort(list);<br \/>\n\/\/ \u5217\u8868\u72b6\u6001: [1, 2, 3, 4]<br \/>\nSystem.out.println(list); \/\/ [1, 2, 3, 4]<\/p>\n<p>\u2461 sort(List list, Comparator&lt;? super T&gt; c)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u6839\u636e\u6307\u5b9a\u6bd4\u8f83\u5668\u5bf9\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002<\/p>\n<p>List&lt;String&gt; list &#061; new ArrayList&lt;&gt;();<br \/>\nlist.add(&#034;banana&#034;);<br \/>\nlist.add(&#034;apple&#034;);<br \/>\nlist.add(&#034;cherry&#034;);<br \/>\nCollections.sort(list, Comparator.reverseOrder());<br \/>\n\/\/ \u5217\u8868\u72b6\u6001: [cherry, banana, apple]&#xff08;\u964d\u5e8f&#xff09;<br \/>\nSystem.out.println(list); \/\/ [cherry, banana, apple]<\/p>\n<p>\/\/ \u6309\u5b57\u7b26\u4e32\u957f\u5ea6\u6392\u5e8f<br \/>\nCollections.sort(list, (a, b) -&gt; a.length() &#8211; b.length());<br \/>\n\/\/ \u5217\u8868\u72b6\u6001: [apple, banana, cherry]&#xff08;\u6309\u957f\u5ea6\u5347\u5e8f&#xff09;<br \/>\nSystem.out.println(list); \/\/ [apple, banana, cherry]<\/p>\n<p>\u2462 reverse(List&lt;?&gt; list)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u53cd\u8f6c\u5217\u8868\u4e2d\u5143\u7d20\u7684\u987a\u5e8f\u3002<\/p>\n<p>List&lt;Integer&gt; list &#061; new ArrayList&lt;&gt;();<br \/>\nlist.add(1);<br \/>\nlist.add(2);<br \/>\nlist.add(3);<br \/>\nlist.add(4);<br \/>\nCollections.reverse(list);<br \/>\n\/\/ \u5217\u8868\u72b6\u6001: [4, 3, 2, 1]<br \/>\nSystem.out.println(list); \/\/ [4, 3, 2, 1]<\/p>\n<p>\u2463 shuffle(List&lt;?&gt; list)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u968f\u673a\u6253\u4e71\u5217\u8868\u4e2d\u5143\u7d20\u7684\u987a\u5e8f&#xff08;\u4f7f\u7528\u9ed8\u8ba4\u968f\u673a\u6e90&#xff09;\u3002<\/p>\n<p>List&lt;Integer&gt; list &#061; new ArrayList&lt;&gt;();<br \/>\nlist.add(1);<br \/>\nlist.add(2);<br \/>\nlist.add(3);<br \/>\nlist.add(4);<br \/>\nCollections.shuffle(list);<br \/>\n\/\/ \u5217\u8868\u72b6\u6001: \u6bcf\u6b21\u7ed3\u679c\u4e0d\u540c&#xff0c;\u5982 [2, 4, 1, 3]<br \/>\nSystem.out.println(list); \/\/ \u53ef\u80fd\u662f [2, 4, 1, 3]<\/p>\n<p>\u2464 shuffle(List&lt;?&gt; list, Random rnd)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u4f7f\u7528\u6307\u5b9a\u7684\u968f\u673a\u6e90\u968f\u673a\u6253\u4e71\u5217\u8868\u3002<\/p>\n<p>List&lt;Integer&gt; list &#061; new ArrayList&lt;&gt;();<br \/>\nlist.add(1);<br \/>\nlist.add(2);<br \/>\nlist.add(3);<br \/>\nlist.add(4);<br \/>\nRandom random &#061; new Random(123); \/\/ \u56fa\u5b9a\u79cd\u5b50&#xff0c;\u6bcf\u6b21\u7ed3\u679c\u76f8\u540c<br \/>\nCollections.shuffle(list, random);<br \/>\n\/\/ \u5217\u8868\u72b6\u6001: [3, 4, 2, 1]&#xff08;\u56fa\u5b9a\u79cd\u5b50\u4fdd\u8bc1\u53ef\u91cd\u73b0&#xff09;<br \/>\nSystem.out.println(list); \/\/ [3, 4, 2, 1]<\/p>\n<p>\u2465 swap(List&lt;?&gt; list, int i, int j)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u4ea4\u6362\u5217\u8868\u4e2d\u6307\u5b9a\u4f4d\u7f6e\u7684\u4e24\u4e2a\u5143\u7d20\u3002<\/p>\n<p>List&lt;Integer&gt; list &#061; new ArrayList&lt;&gt;();<br \/>\nlist.add(1);<br \/>\nlist.add(2);<br \/>\nlist.add(3);<br \/>\nlist.add(4);<br \/>\nCollections.swap(list, 0, 3);<br \/>\n\/\/ \u5217\u8868\u72b6\u6001: [4, 2, 3, 1]&#xff08;\u4ea4\u6362\u7d22\u5f150\u548c3&#xff09;<br \/>\nSystem.out.println(list); \/\/ [4, 2, 3, 1]<\/p>\n<p>\u2466 binarySearch(List&lt;? extends Comparable&lt;? super T&gt;&gt; list, T key)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u4f7f\u7528\u4e8c\u5206\u67e5\u627e\u6cd5\u5728\u5df2\u6392\u5e8f\u7684\u5217\u8868\u4e2d\u67e5\u627e\u6307\u5b9a\u5143\u7d20\u3002\u8fd4\u56de\u7d22\u5f15&#xff0c;\u82e5\u627e\u4e0d\u5230\u8fd4\u56de -(\u63d2\u5165\u70b9) &#8211; 1\u3002<\/p>\n<p>List&lt;Integer&gt; list &#061; new ArrayList&lt;&gt;();<br \/>\nlist.add(1);<br \/>\nlist.add(3);<br \/>\nlist.add(5);<br \/>\nlist.add(7);<br \/>\nlist.add(9);<br \/>\n\/\/ \u5fc5\u987b\u63d0\u524d\u6392\u5e8f&#xff01;<br \/>\nCollections.sort(list); \/\/ [1, 3, 5, 7, 9]<\/p>\n<p>int index &#061; Collections.binarySearch(list, 5);<br \/>\n\/\/ \u8fd4\u56de 2&#xff08;\u7d22\u5f15\u4f4d\u7f6e&#xff09;<br \/>\nSystem.out.println(index); \/\/ 2<\/p>\n<p>int notFound &#061; Collections.binarySearch(list, 6);<br \/>\n\/\/ \u8fd4\u56de -4&#xff08;\u63d2\u5165\u70b9\u7d22\u5f153&#xff0c;\u6240\u4ee5 -(3) &#8211; 1 &#061; -4&#xff09;<br \/>\nSystem.out.println(notFound); \/\/ -4<\/p>\n<p>\u2467 max(Collection&lt;? extends T&gt; coll)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u8fd4\u56de\u96c6\u5408\u4e2d\u7684\u6700\u5927\u5143\u7d20&#xff08;\u6839\u636e\u81ea\u7136\u6392\u5e8f&#xff09;\u3002<\/p>\n<p>List&lt;Integer&gt; list &#061; new ArrayList&lt;&gt;();<br \/>\nlist.add(3);<br \/>\nlist.add(7);<br \/>\nlist.add(1);<br \/>\nlist.add(9);<br \/>\nInteger max &#061; Collections.max(list);<br \/>\n\/\/ \u8fd4\u56de 9<br \/>\nSystem.out.println(max); \/\/ 9<\/p>\n<p>\u2468 max(Collection&lt;? extends T&gt; coll, Comparator&lt;? super T&gt; comp)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u6839\u636e\u6307\u5b9a\u6bd4\u8f83\u5668\u8fd4\u56de\u96c6\u5408\u4e2d\u7684\u6700\u5927\u5143\u7d20\u3002<\/p>\n<p>List&lt;String&gt; list &#061; new ArrayList&lt;&gt;();<br \/>\nlist.add(&#034;apple&#034;);<br \/>\nlist.add(&#034;banana&#034;);<br \/>\nlist.add(&#034;cherry&#034;);<br \/>\n\/\/ \u6309\u5b57\u7b26\u4e32\u957f\u5ea6\u627e\u6700\u5927<br \/>\nString max &#061; Collections.max(list, (a, b) -&gt; a.length() &#8211; b.length());<br \/>\n\/\/ \u8fd4\u56de &#034;banana&#034; \u6216 &#034;cherry&#034;&#xff08;\u957f\u5ea6\u90fd\u662f6&#xff0c;\u8fd4\u56de\u7b2c\u4e00\u4e2a&#xff09;<br \/>\nSystem.out.println(max); \/\/ banana<\/p>\n<p>\u2469 min(Collection&lt;? extends T&gt; coll)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u8fd4\u56de\u96c6\u5408\u4e2d\u7684\u6700\u5c0f\u5143\u7d20&#xff08;\u6839\u636e\u81ea\u7136\u6392\u5e8f&#xff09;\u3002<\/p>\n<p>List&lt;Integer&gt; list &#061; new ArrayList&lt;&gt;();<br \/>\nlist.add(3);<br \/>\nlist.add(7);<br \/>\nlist.add(1);<br \/>\nlist.add(9);<br \/>\nInteger min &#061; Collections.min(list);<br \/>\n\/\/ \u8fd4\u56de 1<br \/>\nSystem.out.println(min); \/\/ 1<\/p>\n<p>\u246a frequency(Collection&lt;?&gt; c, Object o)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u8fd4\u56de\u6307\u5b9a\u5143\u7d20\u5728\u96c6\u5408\u4e2d\u51fa\u73b0\u7684\u6b21\u6570\u3002<\/p>\n<p>List&lt;Integer&gt; list &#061; new ArrayList&lt;&gt;();<br \/>\nlist.add(1);<br \/>\nlist.add(2);<br \/>\nlist.add(1);<br \/>\nlist.add(3);<br \/>\nlist.add(1);<br \/>\nint count &#061; Collections.frequency(list, 1);<br \/>\n\/\/ \u8fd4\u56de 3&#xff08;1\u51fa\u73b0\u4e863\u6b21&#xff09;<br \/>\nSystem.out.println(count); \/\/ 3<\/p>\n<p>\u246b fill(List&lt;? super T&gt; list, T obj)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u7528\u6307\u5b9a\u5143\u7d20\u66ff\u6362\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u3002<\/p>\n<p>List&lt;Integer&gt; list &#061; new ArrayList&lt;&gt;();<br \/>\nlist.add(1);<br \/>\nlist.add(2);<br \/>\nlist.add(3);<br \/>\nCollections.fill(list, 99);<br \/>\n\/\/ \u5217\u8868\u72b6\u6001: [99, 99, 99]<br \/>\nSystem.out.println(list); \/\/ [99, 99, 99]<\/p>\n<p>\u246c copy(List&lt;? super T&gt; dest, List&lt;? extends T&gt; src)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u5c06\u6e90\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u590d\u5236\u5230\u76ee\u6807\u5217\u8868\u3002\u76ee\u6807\u5217\u8868\u957f\u5ea6\u5fc5\u987b &gt;&#061; \u6e90\u5217\u8868\u3002<\/p>\n<p>List&lt;Integer&gt; src &#061; new ArrayList&lt;&gt;();<br \/>\nsrc.add(1);<br \/>\nsrc.add(2);<br \/>\nsrc.add(3);<\/p>\n<p>List&lt;Integer&gt; dest &#061; new ArrayList&lt;&gt;();<br \/>\ndest.add(0);<br \/>\ndest.add(0);<br \/>\ndest.add(0);<br \/>\ndest.add(0);<br \/>\ndest.add(0);<\/p>\n<p>Collections.copy(dest, src);<br \/>\n\/\/ \u5217\u8868\u72b6\u6001: [1, 2, 3, 0, 0]&#xff08;\u524d3\u4e2a\u88ab\u66ff\u6362&#xff09;<br \/>\nSystem.out.println(dest); \/\/ [1, 2, 3, 0, 0]<\/p>\n<p>\u246d addAll(Collection&lt;? super T&gt; c, T&#8230; elements)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u5c06\u6240\u6709\u6307\u5b9a\u5143\u7d20\u6dfb\u52a0\u5230\u96c6\u5408\u4e2d\u3002<\/p>\n<p>List&lt;Integer&gt; list &#061; new ArrayList&lt;&gt;();<br \/>\nlist.add(1);<br \/>\nlist.add(2);<br \/>\nCollections.addAll(list, 3, 4, 5);<br \/>\n\/\/ \u5217\u8868\u72b6\u6001: [1, 2, 3, 4, 5]<br \/>\nSystem.out.println(list); \/\/ [1, 2, 3, 4, 5]<\/p>\n<p>\u246e replaceAll(List list, T oldVal, T newVal)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u5c06\u5217\u8868\u4e2d\u6240\u6709\u51fa\u73b0\u7684\u65e7\u503c\u66ff\u6362\u4e3a\u65b0\u503c\u3002<\/p>\n<p>List&lt;String&gt; list &#061; new ArrayList&lt;&gt;();<br \/>\nlist.add(&#034;apple&#034;);<br \/>\nlist.add(&#034;banana&#034;);<br \/>\nlist.add(&#034;apple&#034;);<br \/>\nlist.add(&#034;cherry&#034;);<br \/>\nCollections.replaceAll(list, &#034;apple&#034;, &#034;orange&#034;);<br \/>\n\/\/ \u5217\u8868\u72b6\u6001: [orange, banana, orange, cherry]<br \/>\nSystem.out.println(list); \/\/ [orange, banana, orange, cherry]<\/p>\n<p>\u246f rotate(List&lt;?&gt; list, int distance)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u5c06\u5217\u8868\u4e2d\u7684\u5143\u7d20\u5faa\u73af\u79fb\u52a8\u6307\u5b9a\u8ddd\u79bb\u3002\u6b63\u6570\u5411\u53f3\u79fb&#xff0c;\u8d1f\u6570\u5411\u5de6\u79fb\u3002<\/p>\n<p>List&lt;Integer&gt; list &#061; new ArrayList&lt;&gt;();<br \/>\nlist.add(1);<br \/>\nlist.add(2);<br \/>\nlist.add(3);<br \/>\nlist.add(4);<br \/>\nlist.add(5);<\/p>\n<p>Collections.rotate(list, 2); \/\/ \u5411\u53f3\u79fb2\u4f4d<br \/>\n\/\/ \u5217\u8868\u72b6\u6001: [4, 5, 1, 2, 3]<br \/>\nSystem.out.println(list); \/\/ [4, 5, 1, 2, 3]<\/p>\n<p>Collections.rotate(list, -1); \/\/ \u5411\u5de6\u79fb1\u4f4d<br \/>\n\/\/ \u5217\u8868\u72b6\u6001: [5, 1, 2, 3, 4]<br \/>\nSystem.out.println(list); \/\/ [5, 1, 2, 3, 4]<\/p>\n<p>\u2470 disjoint(Collection c1, Collection c2)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u5224\u65ad\u4e24\u4e2a\u96c6\u5408\u662f\u5426\u6ca1\u6709\u4ea4\u96c6\u3002\u6ca1\u6709\u4ea4\u96c6\u8fd4\u56de true&#xff0c;\u6709\u4ea4\u96c6\u8fd4\u56de false\u3002<\/p>\n<p>List&lt;Integer&gt; list1 &#061; new ArrayList&lt;&gt;();<br \/>\nlist1.add(1);<br \/>\nlist1.add(2);<br \/>\nlist1.add(3);<\/p>\n<p>List&lt;Integer&gt; list2 &#061; new ArrayList&lt;&gt;();<br \/>\nlist2.add(4);<br \/>\nlist2.add(5);<br \/>\nlist2.add(6);<\/p>\n<p>List&lt;Integer&gt; list3 &#061; new ArrayList&lt;&gt;();<br \/>\nlist3.add(3);<br \/>\nlist3.add(4);<br \/>\nlist3.add(5);<\/p>\n<p>boolean disjoint1 &#061; Collections.disjoint(list1, list2);<br \/>\n\/\/ \u8fd4\u56de true&#xff08;\u6ca1\u6709\u4ea4\u96c6&#xff09;<br \/>\nSystem.out.println(disjoint1); \/\/ true<\/p>\n<p>boolean disjoint2 &#061; Collections.disjoint(list1, list3);<br \/>\n\/\/ \u8fd4\u56de false&#xff08;\u6709\u4ea4\u96c6&#xff1a;3&#xff09;<br \/>\nSystem.out.println(disjoint2); \/\/ false<\/p>\n<p>\u2471 emptyList() \/ emptySet() \/ emptyMap()<\/p>\n<p>\u7528\u6cd5&#xff1a; \u8fd4\u56de\u4e00\u4e2a\u7a7a\u7684\u4e0d\u53ef\u53d8\u96c6\u5408\u3002\u5e38\u7528\u4e8e\u907f\u514d\u8fd4\u56de null\u3002<\/p>\n<p>\/\/ \u8fd4\u56de\u7a7a\u7684\u4e0d\u53ef\u53d8List<br \/>\nList&lt;String&gt; emptyList &#061; Collections.emptyList();<br \/>\n\/\/ emptyList.add(&#034;a&#034;); \/\/ \u274c \u629b\u51fa UnsupportedOperationException<br \/>\nSystem.out.println(emptyList); \/\/ []<\/p>\n<p>\/\/ \u8fd4\u56de\u7a7a\u7684\u4e0d\u53ef\u53d8Set<br \/>\nSet&lt;Integer&gt; emptySet &#061; Collections.emptySet();<\/p>\n<p>\/\/ \u8fd4\u56de\u7a7a\u7684\u4e0d\u53ef\u53d8Map<br \/>\nMap&lt;String, String&gt; emptyMap &#061; Collections.emptyMap();<\/p>\n<p>\u2472 singletonList(T o) \/ singleton(T o) \/ singletonMap(K key, V value)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u8fd4\u56de\u53ea\u5305\u542b\u4e00\u4e2a\u5143\u7d20\u7684\u4e0d\u53ef\u53d8\u96c6\u5408\u3002<\/p>\n<p>\/\/ \u5355\u5143\u7d20List<br \/>\nList&lt;String&gt; singleList &#061; Collections.singletonList(&#034;hello&#034;);<br \/>\n\/\/ singleList.add(&#034;world&#034;); \/\/ \u274c \u629b\u51fa UnsupportedOperationException<br \/>\nSystem.out.println(singleList); \/\/ [hello]<\/p>\n<p>\/\/ \u5355\u5143\u7d20Set<br \/>\nSet&lt;Integer&gt; singleSet &#061; Collections.singleton(100);<\/p>\n<p>\/\/ \u5355\u5143\u7d20Map<br \/>\nMap&lt;String, Integer&gt; singleMap &#061; Collections.singletonMap(&#034;key&#034;, 123);<br \/>\nSystem.out.println(singleMap); \/\/ {key&#061;123}<\/p>\n<p>\u2473 unmodifiableList(List&lt;? extends T&gt; list) \/ unmodifiableSet() \/ unmodifiableMap()<\/p>\n<p>\u7528\u6cd5&#xff1a; \u8fd4\u56de\u6307\u5b9a\u96c6\u5408\u7684\u4e0d\u53ef\u4fee\u6539\u89c6\u56fe\u3002\u4efb\u4f55\u4fee\u6539\u64cd\u4f5c\u90fd\u4f1a\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<p>List&lt;Integer&gt; original &#061; new ArrayList&lt;&gt;();<br \/>\noriginal.add(1);<br \/>\noriginal.add(2);<br \/>\noriginal.add(3);<\/p>\n<p>List&lt;Integer&gt; unmodifiable &#061; Collections.unmodifiableList(original);<br \/>\nSystem.out.println(unmodifiable); \/\/ [1, 2, 3]<\/p>\n<p>\/\/ unmodifiable.add(4); \/\/ \u274c \u629b\u51fa UnsupportedOperationException<\/p>\n<p>\/\/ \u4f46\u539f\u96c6\u5408\u6539\u53d8\u4f1a\u5f71\u54cd\u4e0d\u53ef\u4fee\u6539\u89c6\u56fe<br \/>\noriginal.add(4);<br \/>\nSystem.out.println(unmodifiable); \/\/ [1, 2, 3, 4]&#xff08;\u89c6\u56fe\u4e5f\u53d8\u4e86&#xff09;<\/p>\n<p>\u3251 synchronizedList(List list) \/ synchronizedSet() \/ synchronizedMap()<\/p>\n<p>\u7528\u6cd5&#xff1a; \u8fd4\u56de\u7ebf\u7a0b\u5b89\u5168\u7684\u96c6\u5408\u3002\u6240\u6709\u65b9\u6cd5\u90fd\u88ab\u540c\u6b65\u4fdd\u62a4\u3002<\/p>\n<p>\/\/ \u666e\u901aList\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684<br \/>\nList&lt;String&gt; list &#061; new ArrayList&lt;&gt;();<\/p>\n<p>\/\/ \u8f6c\u4e3a\u7ebf\u7a0b\u5b89\u5168\u7684List<br \/>\nList&lt;String&gt; syncList &#061; Collections.synchronizedList(list);<\/p>\n<p>\/\/ \u540c\u6837\u9002\u7528\u4e8eSet\u548cMap<br \/>\nSet&lt;String&gt; syncSet &#061; Collections.synchronizedSet(new HashSet&lt;&gt;());<br \/>\nMap&lt;String, String&gt; syncMap &#061; Collections.synchronizedMap(new HashMap&lt;&gt;());<\/p>\n<p>\/\/ \u6ce8\u610f&#xff1a;\u8fed\u4ee3\u65f6\u9700\u8981\u624b\u52a8\u540c\u6b65<br \/>\nsynchronized (syncList) {<br \/>\n    for (String s : syncList) {<br \/>\n        System.out.println(s);<br \/>\n    }<br \/>\n}<\/p>\n<p>\u3252 nCopies(int n, T o)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u8fd4\u56de\u7531 n \u4e2a\u76f8\u540c\u5143\u7d20\u7ec4\u6210\u7684\u4e0d\u53ef\u53d8\u5217\u8868\u3002<\/p>\n<p>List&lt;Integer&gt; list &#061; Collections.nCopies(5, 10);<br \/>\n\/\/ \u5217\u8868\u72b6\u6001: [10, 10, 10, 10, 10]<br \/>\nSystem.out.println(list); \/\/ [10, 10, 10, 10, 10]<br \/>\n\/\/ list.add(20); \/\/ \u274c \u629b\u51fa UnsupportedOperationException<\/p>\n<p>\u3253 enumeration(Collection c)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u8fd4\u56de\u96c6\u5408\u7684\u65e7\u7248\u8fed\u4ee3\u5668 Enumeration\u3002<\/p>\n<p>List&lt;String&gt; list &#061; new ArrayList&lt;&gt;();<br \/>\nlist.add(&#034;a&#034;);<br \/>\nlist.add(&#034;b&#034;);<br \/>\nlist.add(&#034;c&#034;);<\/p>\n<p>Enumeration&lt;String&gt; enumeration &#061; Collections.enumeration(list);<br \/>\nwhile (enumeration.hasMoreElements()) {<br \/>\n    System.out.println(enumeration.nextElement());<br \/>\n}<br \/>\n\/\/ \u8f93\u51fa:<br \/>\n\/\/ a<br \/>\n\/\/ b<br \/>\n\/\/ c<\/p>\n<p>\u3254 list(Enumeration e)<\/p>\n<p>\u7528\u6cd5&#xff1a; \u5c06 Enumeration \u8f6c\u6362\u4e3a ArrayList\u3002<\/p>\n<p>Vector&lt;String&gt; vector &#061; new Vector&lt;&gt;();<br \/>\nvector.add(&#034;x&#034;);<br \/>\nvector.add(&#034;y&#034;);<br \/>\nvector.add(&#034;z&#034;);<\/p>\n<p>Enumeration&lt;String&gt; enumObj &#061; vector.elements();<br \/>\nList&lt;String&gt; list &#061; Collections.list(enumObj);<br \/>\n\/\/ \u5217\u8868\u72b6\u6001: [x, y, z]<br \/>\nSystem.out.println(list); \/\/ [x, y, z]<\/p>\n<p>\u3255 checkedList(List list, Class type) \/ checkedSet() \/ checkedMap()<\/p>\n<p>\u7528\u6cd5&#xff1a; \u8fd4\u56de\u7c7b\u578b\u5b89\u5168\u7684\u96c6\u5408&#xff0c;\u5728\u8fd0\u884c\u65f6\u68c0\u67e5\u6dfb\u52a0\u7684\u5143\u7d20\u7c7b\u578b\u3002<\/p>\n<p>\/\/ \u666e\u901aList\u53ef\u4ee5\u6df7\u5165\u4e0d\u540c\u7c7b\u578b<br \/>\nList rawList &#061; new ArrayList();<br \/>\nrawList.add(&#034;hello&#034;);<br \/>\nrawList.add(123); \/\/ \u53ef\u4ee5\u6df7\u5165<\/p>\n<p>\/\/ \u7c7b\u578b\u5b89\u5168\u7684List<br \/>\nList&lt;String&gt; safeList &#061; Collections.checkedList(<br \/>\n    new ArrayList&lt;&gt;(),<br \/>\n    String.class<br \/>\n);<br \/>\nsafeList.add(&#034;hello&#034;);<br \/>\n\/\/ safeList.add(123); \/\/ \u274c \u629b\u51fa ClassCastException<\/p>\n<p>\u5feb\u901f\u53c2\u8003\u8868<\/p>\n<table>\n<tr>\u65b9\u6cd5\u4f5c\u7528\u793a\u4f8b<\/tr>\n<tbody>\n<tr>\n<td>sort()<\/td>\n<td>\u5347\u5e8f\u6392\u5e8f<\/td>\n<td>Collections.sort(list)<\/td>\n<\/tr>\n<tr>\n<td>reverse()<\/td>\n<td>\u53cd\u8f6c\u987a\u5e8f<\/td>\n<td>Collections.reverse(list)<\/td>\n<\/tr>\n<tr>\n<td>shuffle()<\/td>\n<td>\u968f\u673a\u6253\u4e71<\/td>\n<td>Collections.shuffle(list)<\/td>\n<\/tr>\n<tr>\n<td>swap()<\/td>\n<td>\u4ea4\u6362\u5143\u7d20<\/td>\n<td>Collections.swap(list, 0, 1)<\/td>\n<\/tr>\n<tr>\n<td>binarySearch()<\/td>\n<td>\u4e8c\u5206\u67e5\u627e<\/td>\n<td>Collections.binarySearch(list, key)<\/td>\n<\/tr>\n<tr>\n<td>max()\/min()<\/td>\n<td>\u6700\u5927\/\u6700\u5c0f\u503c<\/td>\n<td>Collections.max(list)<\/td>\n<\/tr>\n<tr>\n<td>frequency()<\/td>\n<td>\u51fa\u73b0\u6b21\u6570<\/td>\n<td>Collections.frequency(list, obj)<\/td>\n<\/tr>\n<tr>\n<td>fill()<\/td>\n<td>\u586b\u5145\u5143\u7d20<\/td>\n<td>Collections.fill(list, 99)<\/td>\n<\/tr>\n<tr>\n<td>copy()<\/td>\n<td>\u590d\u5236\u5217\u8868<\/td>\n<td>Collections.copy(dest, src)<\/td>\n<\/tr>\n<tr>\n<td>addAll()<\/td>\n<td>\u6279\u91cf\u6dfb\u52a0<\/td>\n<td>Collections.addAll(list, 1,2,3)<\/td>\n<\/tr>\n<tr>\n<td>replaceAll()<\/td>\n<td>\u66ff\u6362\u5143\u7d20<\/td>\n<td>Collections.replaceAll(list, old, new)<\/td>\n<\/tr>\n<tr>\n<td>rotate()<\/td>\n<td>\u5faa\u73af\u79fb\u52a8<\/td>\n<td>Collections.rotate(list, 2)<\/td>\n<\/tr>\n<tr>\n<td>disjoint()<\/td>\n<td>\u5224\u65ad\u65e0\u4ea4\u96c6<\/td>\n<td>Collections.disjoint(list1, list2)<\/td>\n<\/tr>\n<tr>\n<td>emptyList()<\/td>\n<td>\u7a7a\u5217\u8868<\/td>\n<td>Collections.emptyList()<\/td>\n<\/tr>\n<tr>\n<td>singletonList()<\/td>\n<td>\u5355\u5143\u7d20\u5217\u8868<\/td>\n<td>Collections.singletonList(obj)<\/td>\n<\/tr>\n<tr>\n<td>nCopies()<\/td>\n<td>n\u4e2a\u91cd\u590d\u5143\u7d20<\/td>\n<td>Collections.nCopies(5, 10)<\/td>\n<\/tr>\n<tr>\n<td>unmodifiableList()<\/td>\n<td>\u4e0d\u53ef\u53d8\u5217\u8868<\/td>\n<td>Collections.unmodifiableList(list)<\/td>\n<\/tr>\n<tr>\n<td>synchronizedList()<\/td>\n<td>\u7ebf\u7a0b\u5b89\u5168\u5217\u8868<\/td>\n<td>Collections.synchronizedList(list)<\/td>\n<\/tr>\n<tr>\n<td>checkedList()<\/td>\n<td>\u7c7b\u578b\u5b89\u5168\u5217\u8868<\/td>\n<td>Collections.checkedList(list, String.class)<\/td>\n<\/tr>\n<tr>\n<td>enumeration()<\/td>\n<td>\u8f6c\u4e3aEnumeration<\/td>\n<td>Collections.enumeration(list)<\/td>\n<\/tr>\n<tr>\n<td>list()<\/td>\n<td>Enumeration\u8f6cList<\/td>\n<td>Collections.list(enumeration)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>import java.util.Collections;\u2460 sort(List list)\u7528\u6cd5&#xff1a; \u5bf9\u5217\u8868\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f&#xff08;\u81ea\u7136\u6392\u5e8f&#xff09;\u3002\u5217\u8868\u5143\u7d20\u5fc5\u987b\u5b9e\u73b0 Comparable \u63a5\u53e3\u3002List list  new ArrayList();<br \/>\nlist.add(3);<br \/>\nlist.add(1);<br \/>\nlist.add(4);<br \/>\nlist.add(2);<br \/>\nCollections.sort(list);<br \/>\n\/\/ \u5217\u8868\u72b6\u6001: [1, 2,<\/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":[10187,149],"topic":[],"class_list":["post-94062","post","type-post","status-publish","format-standard","hentry","category-server","tag-codingplan","tag-windows"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Collections \u5de5\u5177\u7c7b\u5e38\u7528\u65b9\u6cd5\u5168\u89e3 - \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\/94062.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Collections \u5de5\u5177\u7c7b\u5e38\u7528\u65b9\u6cd5\u5168\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"import java.util.Collections;\u2460 sort(List list)\u7528\u6cd5&#xff1a; \u5bf9\u5217\u8868\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f&#xff08;\u81ea\u7136\u6392\u5e8f&#xff09;\u3002\u5217\u8868\u5143\u7d20\u5fc5\u987b\u5b9e\u73b0 Comparable \u63a5\u53e3\u3002List list new ArrayList(); list.add(3); list.add(1); list.add(4); list.add(2); Collections.sort(list); \/\/ \u5217\u8868\u72b6\u6001: [1, 2,\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/94062.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-13T08:16:45+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=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/94062.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/94062.html\",\"name\":\"Collections \u5de5\u5177\u7c7b\u5e38\u7528\u65b9\u6cd5\u5168\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-13T08:16:45+00:00\",\"dateModified\":\"2026-08-13T08:16:45+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/94062.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/94062.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/94062.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Collections \u5de5\u5177\u7c7b\u5e38\u7528\u65b9\u6cd5\u5168\u89e3\"}]},{\"@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":"Collections \u5de5\u5177\u7c7b\u5e38\u7528\u65b9\u6cd5\u5168\u89e3 - \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\/94062.html","og_locale":"zh_CN","og_type":"article","og_title":"Collections \u5de5\u5177\u7c7b\u5e38\u7528\u65b9\u6cd5\u5168\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"import java.util.Collections;\u2460 sort(List list)\u7528\u6cd5&#xff1a; \u5bf9\u5217\u8868\u8fdb\u884c\u5347\u5e8f\u6392\u5e8f&#xff08;\u81ea\u7136\u6392\u5e8f&#xff09;\u3002\u5217\u8868\u5143\u7d20\u5fc5\u987b\u5b9e\u73b0 Comparable \u63a5\u53e3\u3002List list new ArrayList(); list.add(3); list.add(1); list.add(4); list.add(2); Collections.sort(list); \/\/ \u5217\u8868\u72b6\u6001: [1, 2,","og_url":"https:\/\/www.wsisp.com\/helps\/94062.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-13T08:16:45+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/94062.html","url":"https:\/\/www.wsisp.com\/helps\/94062.html","name":"Collections \u5de5\u5177\u7c7b\u5e38\u7528\u65b9\u6cd5\u5168\u89e3 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-13T08:16:45+00:00","dateModified":"2026-08-13T08:16:45+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/94062.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/94062.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/94062.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Collections \u5de5\u5177\u7c7b\u5e38\u7528\u65b9\u6cd5\u5168\u89e3"}]},{"@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\/94062","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=94062"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/94062\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=94062"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=94062"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=94062"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=94062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}