{"id":710,"date":"2025-02-06T18:17:51","date_gmt":"2025-02-06T10:17:51","guid":{"rendered":"https:\/\/vite66.cn\/?p=710"},"modified":"2025-03-13T18:32:57","modified_gmt":"2025-03-13T10:32:57","slug":"java-%e5%b8%b8%e7%94%a8%e9%9b%86%e5%90%88","status":"publish","type":"post","link":"https:\/\/vite66.cn\/?p=710","title":{"rendered":"Java \u5e38\u7528\u96c6\u5408"},"content":{"rendered":"<h3><strong>\u7ebf\u6027\u7ed3\u6784<\/strong><\/h3>\n<h4><strong>ArrayList<\/strong><\/h4>\n<ul>\n<li><strong>\u7279\u6027<\/strong>\n<ul>\n<li>\u52a8\u6001\u6570\u7ec4\u5b9e\u73b0<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5e38\u7528\u65b9\u6cd5<\/strong>\n<pre><code class=\"language-Java\">List&lt;Integer&gt; list = new ArrayList&lt;&gt;(100);  \/\/ \u9884\u5206\u914d\u5bb9\u91cf\nlist.add(e);  \/\/ \u589e\u52a0\u5143\u7d20\nlist.get(index); \/\/ \u6839\u636e\u4e0b\u6807\u83b7\u53d6\u5143\u7d20\nlist.set(index, e); \/\/ \u6307\u5b9a\u7684\u4e0b\u6807 index \u5904\u8bbe\u7f6e\u4e00\u4e2a\u65b0\u7684\u5143\u7d20 e\uff0c\u5e76\u8fd4\u56de\u539f\u6765\u5728\u8be5\u4f4d\u7f6e\u7684\u5143\u7d20\u3002\nlist.remove(index); \/\/ \u5220\u9664\u4e0b\u6807\u5143\u7d20\nlist.size() \/\/ \u83b7\u53d6\u5927\u5c0f<\/code><\/pre>\n<\/li>\n<\/ul>\n<h4><strong>LinkedList<\/strong><\/h4>\n<ul>\n<li><strong>\u7279\u6027<\/strong>\n<ul>\n<li>\u53cc\u5411\u94fe\u8868\u5b9e\u73b0<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5e38\u7528\u65b9\u6cd5<\/strong>  <\/li>\n<\/ul>\n<pre><code>  LinkedList&lt;Integer&gt; list = new LinkedList&lt;&gt;();\n  list.addFirst(e); \/\/ \u961f\u9996\u63d2\u5165\n  list.addLast(e); \/\/ \u961f\u5c3e\u63d2\u5165\n  list.pollFirst(); \/\/ \u5f39\u51fa\u961f\u9996\n  list.pollLast(); \/\/ \u5f39\u51fa\u961f\u5c3e\n  list.pollLast(); \/\/ \u67e5\u770b\u961f\u5c3e\n  list.isEmpty(); \/\/ \u5224\u65ad\u975e\u7a7a<\/code><\/pre>\n<h3><strong>\u961f\u5217\u4e0e\u53cc\u7aef\u961f\u5217<\/strong><\/h3>\n<h4><strong> ArrayDeque<\/strong><\/h4>\n<ul>\n<li><strong>\u7279\u6027<\/strong>\n<ul>\n<li>\u5faa\u73af\u6570\u7ec4\u5b9e\u73b0<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5e38\u7528\u65b9\u6cd5<\/strong>\n<pre><code class=\"language-java\">Deque&lt;Integer&gt; deque = new ArrayDeque&lt;&gt;();\ndeque.offerFirst(e); \/\/ \u961f\u9996\u63d2\u5165\ndeque.offerLast(e); \/\/ \u961f\u5c3e\u63d2\u5165\ndeque.pollFirst(); \/\/ \u5f39\u51fa\u961f\u9996\ndeque.pollLast(); \/\/ \u5f39\u51fa\u961f\u5c3e\ndeque.peekLast(); \/\/ \u67e5\u770b\u961f\u5c3e\ndeque.peekFirst(); \/\/ \u67e5\u770b\u961f\u9996\ndeque.size(); \/\/ \u5927\u5c0f\ndeque.isEmpty() \/\/\u68c0\u67e5\u961f\u5217\u662f\u5426\u4e3a\u7a7a\ndeque.contains(Object o) \/\/\u68c0\u67e5\u961f\u5217\u662f\u5426\u5305\u542b\u67d0\u4e2a\u5143\u7d20\ndeque.remove(Object o) \/\/\u5220\u9664\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u5143\u7d20\uff08\u4ece\u961f\u9996\u5230\u961f\u5c3e\u904d\u5386\uff09\n\/\/ \u5f53\u666e\u901a\u961f\u5217\u65f6\u53ef\u4ee5\u76f4\u63a5\u7528 \nadd(); \/\/ \u52a0\u5165\npeek()\uff1b\/\/ \u67e5\u770b\npoll(); \/\/ \u67e5\u770b\u5e76\u5f39\u51fa<\/code><\/pre>\n<\/li>\n<\/ul>\n<h4><strong> PriorityQueue<\/strong><\/h4>\n<ul>\n<li><strong>\u7279\u6027<\/strong>\n<ul>\n<li>\u5806\u5b9e\u73b0\uff08\u9ed8\u8ba4\u6700\u5c0f\u5806\uff09<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5e38\u7528\u65b9\u6cd5<\/strong>\n<pre><code class=\"language-java\">Queue&lt;Integer&gt; pq = new PriorityQueue&lt;&gt;((a,b) -&gt; b - a); \/\/ \u6700\u5927\u5806\npq.offer(e); \npq.peek(); \npq.poll();<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3><strong>Set\u7c7b<\/strong><\/h3>\n<h4><strong> HashSet<\/strong><\/h4>\n<ul>\n<li><strong>\u7279\u6027<\/strong>\n<ul>\n<li>\u54c8\u5e0c\u8868\u5b9e\u73b0\uff0c\u65e0\u5e8f\uff0c\u4e0d\u91cd\u590d\uff0c\u65e0\u7d22\u5f15<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5e38\u7528\u65b9\u6cd5<\/strong>\n<pre><code class=\"language-java\">Set&lt;Integer&gt; set = new HashSet&lt;&gt;();\nset.add(e); \nset.contains(e); \nset.remove(e);<\/code><\/pre>\n<h4><strong>TreeSet<\/strong><\/h4>\n<\/li>\n<li><strong>\u7279\u6027<\/strong>\n<ul>\n<li>\u7ea2\u9ed1\u6811\u5b9e\u73b0\uff0c\u6392\u5e8f\uff0c\u4e0d\u91cd\u590d\uff0c\u65e0\u7d22\u5f15<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u9ad8\u7ea7\u65b9\u6cd5<\/strong>\n<pre><code class=\"language-java\">TreeSet&lt;Integer&gt; set = new TreeSet&lt;&gt;();\nset.ceiling(e);  \/\/ \u6700\u5c0f &gt;= e \u7684\u5143\u7d20\nset.floor(e);    \/\/ \u6700\u5927 &lt;= e \u7684\u5143\u7d20\nset.higher(e);   \/\/ \u6700\u5c0f &gt; e \u7684\u5143\u7d20<\/code><\/pre>\n<\/li>\n<\/ul>\n<h3><strong>\u6620\u5c04\u8868<\/strong><\/h3>\n<h4><strong> HashMap<\/strong><\/h4>\n<ul>\n<li><strong>\u7279\u6027<\/strong>\n<ul>\n<li>\u54c8\u5e0c\u8868\u5b9e\u73b0\uff0c\u63d2\u5165\/\u67e5\u627e O(1)<\/li>\n<li>\u5141\u8bb8 null \u952e\/\u503c<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u5e38\u7528\u65b9\u6cd5<\/strong>\n<pre><code class=\"language-java\">\nMap&lt;String, Integer&gt; map = new HashMap&lt;&gt;();\nmap.put(key, val); \/\/ \u91cd\u590d key \u5219\u8986\u76d6\nmap.get(key);\nmap.containsKey(key);\nmap.containsValue(Object value)\uff1b\nmap.getOrDefault(key, 0);\nmap.putIfAbsent(K key, V value);\/\/ \u952e\u4e0d\u5b58\u5728\u5219\u63d2\u5165\nmap.remove(Object key)\nkeySet()\nvalues()\nentrySet()\nisEmpty()<\/code><\/pre>\n<\/li>\n<\/ul>\n<pre><code>\n#### **TreeMap**\n- **\u7279\u6027**  \n  - \u7ea2\u9ed1\u6811\u5b9e\u73b0\uff0c\u6309\u952e\u81ea\u7136\u987a\u5e8f\u6392\u5e8f\n\n- **\u9ad8\u7ea7\u65b9\u6cd5**  \n```java\n  TreeMap&lt;Integer, String&gt; map = new TreeMap&lt;&gt;();\n  map.firstKey(); map.lastKey();\n  map.lowerEntry(k);  \/\/ \u6700\u5927\u952e &lt; k\n  map.tailMap(k);     \/\/ \u6240\u6709\u952e &gt;= k<\/code><\/pre>\n<h3><strong>\u5de5\u5177\u7c7b<\/strong><\/h3>\n<h4><strong> Arrays<\/strong><\/h4>\n<ul>\n<li><strong>\u6838\u5fc3\u65b9\u6cd5<\/strong>\n<pre><code class=\"language-java\">int[] arr = new int[10];\nArrays.sort(arr);  \/\/ \u5feb\u901f\u6392\u5e8f\nArrays.fill(arr, 0);  \/\/ \u586b\u5145\u503c\nArrays.binarySearch(arr, key); \/\/ \u4e8c\u5206\u67e5\u627e\uff08\u5fc5\u987b\u6709\u5e8f\uff01\uff09<\/code><\/pre>\n<\/li>\n<\/ul>\n<h4><strong>Collections<\/strong><\/h4>\n<ul>\n<li><strong>\u5e38\u7528\u64cd\u4f5c<\/strong>\n<pre><code class=\"language-java\">Collections.reverse(list);     \/\/ \u53cd\u8f6c\u5217\u8868\nCollections.rotate(list, 2);  \/\/ \u5faa\u73af\u53f3\u79fb2\u4f4d\nCollections.max(list);        \/\/ \u627e\u6700\u5927\u503c\nCollections.shuffle(list); \/\/ \u6253\u4e71\u987a\u5e8f<\/code><\/pre>\n<\/li>\n<\/ul>\n<h4><strong> StringBuilder<\/strong><\/h4>\n<ul>\n<li><strong>\u7279\u6027<\/strong><br \/>\n\u9ad8\u9891\u5b57\u7b26\u4e32\u62fc\u63a5\u65f6\u4f7f\u7528\uff08\u66ff\u4ee3 String\uff09<\/p>\n<pre><code class=\"language-java\">StringBuilder sb = new StringBuilder();\nsb.append(&quot;Hello&quot;).append(123);\nString result = sb.toString();\nsb.reverse(); \/\/ \u7ffb\u8f6c<\/code><\/pre>\n<\/li>\n<\/ul>\n<h4><strong> String<\/strong><\/h4>\n<pre><code class=\"language-java\">s.length() \/\/ \u83b7\u53d6\u957f\u5ea6\ns.charAt(index) \/\/ \u83b7\u53d6 index \u5750\u6807\u4e0b\u5b57\u7b26\ns.indexOf(&quot;el&quot;); \/\/ \u8fd4\u56de\u5b50\u5b57\u7b26\u4e32\u7b2c\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\ns.lastIndexOf(&quot;l&quot;); \/\/ \u8fd4\u56de\u5b50\u5b57\u7b26\u4e32\u6700\u540e\u4e00\u6b21\u51fa\u73b0\u7684\u7d22\u5f15\ns.contains(&quot;ell&quot;); \/\/ \u68c0\u67e5\u5b57\u7b26\u4e32\u662f\u5426\u5305\u542b\u6307\u5b9a\u5b57\u7b26\u5e8f\u5217\ns.equals(&quot;Hello&quot;); \/\/ \u5224\u65ad\u662f\u5426\u76f8\u7b49\ns.equalsIgnoreCase(&quot;hello&quot;); \/\/ \u5ffd\u7565\u5927\u5c0f\u5199\u6bd4\u8f83\ns.compareTo(&quot;Hallo&quot;); \/\/ \u6309\u5b57\u5178\u5e8f\u6bd4\u8f83\ns.substring(1, 4); \/\/ \u622a\u53d6[1, 4) \u5b50\u5b57\u7b26\u4e32\nString.join(&quot;-&quot;, &quot;Java&quot;, &quot;is&quot;, &quot;fun&quot;); \/\/ joined = &quot;Java-is-fun&quot; \u7528\u5236\u5ea6\u5b57\u7b26\u8fdb\u884c\u5206\u5272\ns.concat(&quot; World&quot;); \/\/ \u62fc\u63a5\u5230\u672b\u5c3e\ns.replace(&#039;l&#039;, &#039;L&#039;); \/\/ \u5b57\u7b26\u66ff\u6362\uff0c\u540e\u9762\u66ff\u6362\u524d\u9762\nreplaceAll(String regex, String replacement); \/\/ \u5b57\u7b26\u4e32\u66ff\u6362\n&quot;Java-is-fun&quot;.split(&quot;-&quot;); \/\/ parts = [&quot;Java&quot;, &quot;is&quot;, &quot;fun&quot;] \u5b57\u7b26\u4e32\u5206\u5272\ns.toLowerCase(); \/\/ \u8f6c\u5c0f\u5199\ns.toUpperCase(); \/\/ \u8f6c\u5927\u5199\n&quot;  Hello  &quot;.trim(); \/\/ trimmed = &quot;Hello&quot; \u53bb\u9664\u7a7a\u767d\nString.format(&quot;Name: %s, Age: %d&quot;, &quot;Alice&quot;, 25); \/\/ formatted = &quot;Name: Alice, Age: 25&quot; \u683c\u5f0f\u5316\ns.isEmpty(); \/\/ \u5224\u65ad\u662f\u5426\u4e3a\u7a7a\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u7ebf\u6027\u7ed3\u6784 ArrayList \u7279\u6027 \u52a8\u6001\u6570\u7ec4\u5b9e\u73b0 \u5e38\u7528\u65b9\u6cd5 List&lt;Integer&gt; list  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-710","post","type-post","status-publish","format-standard","hentry","category-science-and-engineering"],"_links":{"self":[{"href":"https:\/\/vite66.cn\/index.php?rest_route=\/wp\/v2\/posts\/710","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vite66.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vite66.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vite66.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vite66.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=710"}],"version-history":[{"count":22,"href":"https:\/\/vite66.cn\/index.php?rest_route=\/wp\/v2\/posts\/710\/revisions"}],"predecessor-version":[{"id":751,"href":"https:\/\/vite66.cn\/index.php?rest_route=\/wp\/v2\/posts\/710\/revisions\/751"}],"wp:attachment":[{"href":"https:\/\/vite66.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=710"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vite66.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=710"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vite66.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}