<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Java 中`List`和`Set`之間的區別 > 原文: [https://beginnersbook.com/2014/07/difference-between-list-and-set-in-java/](https://beginnersbook.com/2014/07/difference-between-list-and-set-in-java/) `List`和`Set`都是接口。它們都擴展了`Collection`接口。在這篇文章中,我們討論了 java 中`List`和`Set`接口之間的**差異。** ## `List` Vs `Set` 1)`List`是一個有序集合,它維護插入順序,這意味著在顯示列表內容時,它將按照它們插入列表的順序顯示元素。 `Set`是一個無序集合,它不保持任何順序。 維護順序的`Set`很少,例如`LinkedHashSet`(它按插入順序維護元素)。 2)`List`允許重復,而`Set`不允許重復元素。如果您嘗試將重復元素插入`Set`中,它將替換現有值,則`Set`的所有元素都應該是唯一的。 3)`List`實現: [`ArrayList`](https://beginnersbook.com/2013/12/java-arraylist/) , [`LinkedList`](https://beginnersbook.com/2013/12/linkedlist-in-java-with-example/)等。 `Set`實現: [`HashSet`](https://beginnersbook.com/2013/12/hashset-class-in-java-with-example/), [`LinkedHashSet`](https://beginnersbook.com/2013/12/linkedhashset-class-in-java-with-example/), [`TreeSet`](https://beginnersbook.com/2013/12/treeset-class-in-java-with-example/) 等。 4)`List`允許任意數量的空值。 `Set`最多只能有一個空值。 5) [`ListIterator`](https://beginnersbook.com/2014/06/listiterator-in-java-with-examples/)可用于在兩個方向(前向和后向)遍歷`List`但是它不能用于遍歷`Se`。我們可以使用[迭代器](https://beginnersbook.com/2014/06/java-iterator-with-examples/)(它也適用于`List`)來遍歷一個`Set`。 6)`List`接口有一個名為[`Vector`](https://beginnersbook.com/2013/12/vector-in-java/) 的遺留類,而`Set`接口沒有任何遺留類。 ## 何時使用`Set`和何時使用`List`? 用法完全取決于要求: 如果要求只有唯一值,那么`Set`是您最好的選擇,因為`Set`的任何實現僅維護唯一值。 如果需要維持插入順序而不管雙重性,則`List`是最佳選擇。`List`接口的實現 - `ArrayList`和`LinkedList`按其插入順序對元素進行排序。 #### 列表示例 ```java import java.util.List; import java.util.ArrayList; import java.util.LinkedList; public class ListExample { public static void main(String[] args) { List<String> al = new ArrayList<String>(); al.add("Chaitanya"); al.add("Rahul"); al.add("Ajeet"); System.out.println("ArrayList Elements: "); System.out.print(al); List<String> ll = new LinkedList<String>(); ll.add("Kevin"); ll.add("Peter"); ll.add("Kate"); System.out.println("\nLinkedList Elements: "); System.out.print(ll); } } ``` **輸出:** ```java ArrayList Elements: [Chaitanya, Rahul, Ajeet] LinkedList Elements: [Kevin, Peter, Kate] ``` #### 設置示例 ```java import java.util.Set; import java.util.HashSet; import java.util.TreeSet; public class SetExample { public static void main(String args[]) { int count[] = {11, 22, 33, 44, 55}; Set<Integer> hset = new HashSet<Integer>(); try{ for(int i = 0; i<4; i++){ hset.add(count[i]); } System.out.println(hset); TreeSet<Integer> treeset = new TreeSet<Integer>(hset); System.out.println("The sorted list is:"); System.out.println(treeset); } catch(Exception e){ e.printStackTrace(); } } } ``` **輸出:** ```java [33, 22, 11, 44] The sorted list is: [11, 22, 33, 44] ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看