<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## [Set](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-set/index.html#set)接口 [TOC] ### Set接口簡介 ``` interface Set<out E> : Collection<E> ``` Set接口和List接口一樣,同樣繼承自Collection接口,它與Collection接口中的方法基本一致,但并沒有對Collection接口進行功能上的擴充,只是比Collection接口更加嚴格了。與List接口不同的是,**Set接口中的元素是無序的,并且元素不可重復,重復的元素只會被記錄一次**。 在Kotlin中,Set分為可變集合MutableSet與不可變集合Set,其中可變集合MutableSet是對集合中的元素進行增加和刪除的操作,不可變集合Set對集合中的元素僅提供只讀的操作。 ### Set接口的繼承結構 可參看官方網站set的繼承[Inheritors](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-set/index.html#inheritors) ``` interface Set<out E> : Collection<E> ``` #### AbstractSet [AbstractSet](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-set/index.html#abstractset) ``` abstract class AbstractSet<out E> : AbstractCollection<E>, Set<E> ``` #### MutableSet [MutableSet](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-set/index.html#mutableset) ``` interface MutableSet<E> : Set<E>, MutableCollection<E> ``` * 繼承[Inheritors](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-set/index.html#inheritors) ##### AbstractMutableSet [AbstractMutableSet](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-mutable-set/index.html) ``` abstract class AbstractMutableSet<E> : MutableSet<E> ``` ##### HashSet [HashSet](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-hash-set/index.html#hashset) ``` class HashSet<E> : MutableSet<E>, AbstractMutableCollection<E>, KonanSet<E> ``` ##### LinkedHashSet [LinkedHashSet](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-linked-hash-set/index.html#linkedhashset) ``` typealias LinkedHashSet<V> = HashSet<V> ``` `Set`的默認實現 -[`LinkedHashSet`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-linked-hash-set/index.html)– 保留元素插入的順序。 因此,依賴于順序的函數,例如`first()`或`last()`,會在這些`set`上返回可預測的結果。 ``` fun main() { //sampleStart val numbers = setOf(1, 2, 3, 4) // LinkedHashSet is the default implementation val numbersBackwards = setOf(4, 3, 2, 1) println(numbers.first() == numbersBackwards.first()) println(numbers.first() == numbersBackwards.last()) //sampleEnd } ``` 運行結果 ``` false true ``` 另一種實現方式 –[`HashSet`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-hash-set/index.html)– 不聲明元素的順序,所以在它上面調用這些函數會返回不可預測的結果。但是,`HashSet`只需要較少的內存來存儲相同數量的元素。 ### 不可變Set 不可變Set同樣是繼承了Collection接口,調用標準庫中的setOf()函數來創建不可變Set集合,具體代碼如下: ``` val mSet = setOf(1, 8, 9, 1, 4, 7, 9, 0, 0, 8) println(mSet) ``` 由于Set集合中的元素具有不可重復性,因此上述代碼的運行結果如下: ``` [1, 8,9, 4, 7, 0] ``` 不可變集合Set與不可變集合List類似,都是一個只提供只讀操作的集合,并且Set集合中的操作與List集合也類似,Set集合的主要操作有查詢操作與批量操作。 #### 查詢操作 Set集合的查詢操作主要有判斷集合是否為空、獲取集合中元素的數量、判斷集合中是否包含某一個元素以及返回集合中的元素的迭代器,如表所示。 ![](https://img.kancloud.cn/b1/91/b19183727a55d0061a137a3bc6933178_1371x265.png) #### 批量操作 Set集合中的批量操作的方法只有containsAll(elements:Collection<@UnsafeVariance E>),這個方法的含義是判斷集合中是否包含某一個集合。 >[success]注意:**Set集合中也可以進行List集合中的修改操作(Set集合沒有set之類的方法修改元素)和迭代器([iterator](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-hash-set/iterator.html#iterator))操作**,由于這兩個操作與List集合中內容一樣,因此就不再重復寫一遍了,大家可以根據List接口中的方法來理解Set集合,區別在于set集合元素不可重復。 ### 可變MutableSet MutableSet接口繼承于Set接口與MutableCollection接口,同時對Set接口進行擴展,在該接口中添加了對集合中元素的添加和刪除等操作。可變MutableSet集合是使用mutableSetOf()函數來創建對象的,具體代碼如下: ``` val muSet: MutableSet<Int> = mutableSetOf(1, 2, 3) ``` 上述代碼中創建了一個可變的MutableSet集合,該集合中的操作主要有查詢操作、修改操作、批量操作以及迭代器,由于這些操作與MutableList集合中的操作比較類似,因此只例舉修改操作即可,其他操作可以參考MutableList集合與MutableSet接口中的方法來理解。 MutableSet集合可以對該集合中的元素進行修改操作,這些修改操作主要有向集合中添加元素與移除集合中的元素,如表所示。 ![](https://img.kancloud.cn/0e/c7/0ec7de3a37715eec95e4574ca1b0543a_1372x187.png)
                  <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>

                              哎呀哎呀视频在线观看