📜  Kotlin MutableSet:mutableSetOf()

📅  最后修改于: 2021-01-05 07:52:15             🧑  作者: Mango

Kotlin MutableSet接口

Kotlin MutableSet接口是元素的一般无序集合。 MutableSet接口不支持重复元素。该接口是可变的,因此其方法支持读写功能,支持添加和删除元素。

Set接口使用mutableSetOf()函数创建set接口的对象列表,其中包含元素列表。

MutableSet接口声明

interface MutableSet : Set, MutableCollection (source)

MutableSet接口的继承属性

Properties Description
abstract val size: Int It returns the size of collection.

MutableSet接口的功能

Kotlin MutableSet接口具有多种功能。下面将介绍其某些功能。

Functions Description
abstract fun add(element: E): Boolean It adds the given element to the collection.
abstract fun addAll(elements: Collection): Boolean It adds all the elements given collection to the current collection.
abstract fun clear() It removes all the elements from this collection.
abstract fun iterator(): MutableIterator It returns an iterator over the elements of this object.
abstract fun remove(element: E): Boolean It removes a single specified element from this collection, if it is present in collection.
abstract fun removeAll(elements: Collection): Boolean It removes all the elements from current collection which are given in collection.
abstract fun retainAll(elements: Collection): Boolean It retains only those elements in current collection which are present in specified collection.
abstract fun contains(element: E): Boolean It checks the specified element is contained in current collection.
abstract fun containsAll(elements: Collection): Boolean It checks all the elements of specified collection are present in current collection.
abstract fun isEmpty(): Boolean If collection is empty (not containing any element) it returns true, otherwise it returns false.
fun Iterable.any(): Boolean It returns true if collection contains at least one element.
fun Iterable.any(predicate: (T) -> Boolean): Boolean It returns true if at least element matches the given the given predicate.
fun Iterable.distinct(): List It returns a list which contains only distinct elements from the given collection.
fun Iterable.drop(n: Int): List It returns a list which contains all elements except first n elements.
fun Iterable.elementAt(index: Int): T It returns an element at given index or throw an IndexOutOfBoundException if given index is not present in collection.
fun Iterable.elementAtOrElse(
index: Int,
defaultValue: (Int) -> T
): T
It returns an element at given index or result of calling the defaultValue function if the index is out bounds in current collection.
fun > Iterable.max(): T? It returns the largest element or null if there is no element in the collection.
fun > Iterable.min(): T? It returns the smallest element or null if there is no element in the collection.
fun MutableCollection.remove(element: T): Boolean It removes the single specified element if it in present in current collection.
fun MutableCollection.removeAll(
elements: Collection
): Boolean
It removes all the elements of current collection which are contained in specified collection.
fun MutableCollection.retainAll(
elements: Collection
): Boolean
It retains all the elements in current collection which are contained in specified collection.
fun Iterable.reversed(): List It returns the elements in reversed order.

Kotlin MutableSet接口示例1

让我们创建一个MutableSet声明和遍历其元素的示例。

fun main(args: Array) {
    val intmutableSet = mutableSetOf(2, 6, 4, 29, 4, 5)
    val anymutableSet: Set = setOf(2, 6, 4, 29, 4, 5, "Ajay", "Ashu", "Ajay")
    println("....intmutableSet....")
    for(element in intmutableSet){
        println(element)
    }
    println("....anymutableSet......")
    for(element in anymutableSet){
        println(element)
    }
}

输出:

....intmutableSet....
2
6
4
29
5
....anymutableSet......
2
6
4
29
5
Ajay
Ashu

在上面的示例中,元素“ 4”和“ Ajay”被声明两次。但是在遍历这些MutableSet时,它们仅打印一次,这是因为MutableSet接口不支持重复的元素。

Kotlin MutableSet接口示例2-add()和addAll()

fun main(args: Array) {
    val intmutableSet = mutableSetOf(2, 6, 4, 29, 4, 5)
    val mutableSet: MutableSet = mutableSetOf(6,8,11,22)

    println("....intmutableSet....")
    for(element in intmutableSet){
        println(element)
    }
    intmutableSet.add(10)
    println("....intmutableSet.add(10)....")
    for(element in intmutableSet){
        println(element)
    }

    intmutableSet.addAll(mutableSet)
    println("....intmutableSet.addAll(mutableSet)....")
    for(element in intmutableSet){
        println(element)
    }
}

输出:

....intmutableSet....
2
6
4
29
5
....intmutableSet.add(10)....
2
6
4
29
5
10
....intmutableSet.addAll(mutableSet)....
2
6
4
29
5
10
8
11
22

Kotlin MutableSet接口示例3-remove()和removeAll()

fun main(args: Array) {
    val intmutableSet = mutableSetOf(2, 6, 4, 29, 4, 5)
    val mutableSet: MutableSet = mutableSetOf(6,8,11,22)

    println("....intmutableSet....")
    for(element in intmutableSet){
        println(element)
    }
    intmutableSet.remove(29)
    println("....intmutableSet.remove(29)....")
    for(element in intmutableSet){
        println(element)
    }
    intmutableSet.removeAll(mutableSet)
    println("....intmutableSet.removeAll(mutableSet)....")
    for(element in intmutableSet){
        println(element)
    }
}

输出:

....intmutableSet....
2
6
4
29
5
....intmutableSet.remove(29)....
2
6
4
5
....intmutableSet.removeAll(mutableSet)....
2
4
5

Kotlin MutableSet接口示例4-contains()和containsAll()

fun main(args: Array) {
    val mutableSet1 = mutableSetOf(2, 6, 4, 29, 4, 5)
    val mutableSet2: MutableSet = mutableSetOf(6,8,11,22)
    val mutableSet3: MutableSet = mutableSetOf(2,4,6)

    println("....mutableSet1....")
    for(element in mutableSet1){
        println(element)
    }
    println("....mutableSet2....")
        println(mutableSet2)
    println("....mutableSet3....")
        println(mutableSet3)
    println("....mutableSet1.contains(29)....")
    println(mutableSet1.contains(29))

    println("....mutableSet1.containsAll(mutableSet2))....")
    println(mutableSet1.containsAll(mutableSet2))
    println("....mutableSet1.containsAll(mutableSet3))....")
    println(mutableSet1.containsAll(mutableSet3))
}

输出:

....mutableSet1....
2
6
4
29
5
....mutableSet2....
[6, 8, 11, 22]
....mutableSet3....
[2, 4, 6]
....mutableSet1.contains(29)....
true
....mutableSet1.containsAll(mutableSet2))....
false
....mutableSet1.containsAll(mutableSet3))....
true

Kotlin MutableSet接口示例5-isEmpty()和any()

fun main(args: Array) {
    val mutableSet1 = mutableSetOf(2, 6, 4, 29, 4, 5)

    println("....mutableSet1....")
    for(element in mutableSet1){
        println(element)
    }
    println("....mutableSet1.isEmpty()....")
    if(mutableSet1.isEmpty())
        println("mutableSet1 is empty, not contain any element")
    else
        println("mutableSet1 is not empty, contains element")

    println("....mutableSet1.any()....")
    if(mutableSet1.any())
        println("mutableSet1 contain at least one or more elements")
    else
        println("mutableSet1 not contain any element")
}

输出:

....mutableSet1....
2
6
4
29
5
....mutableSet1.isEmpty()....
mutableSet1 is not empty, contains element
....mutableSet1.any()....
mutableSet1 contain at least one or more elements

Kotlin MutableSet接口示例6-first(),indexOf()和drop()

fun main(args: Array) {
    val mutableSet1 = mutableSetOf(2, 6, 4, 29, 4, 5)

    println("....mutableSet1....")
    for(element in mutableSet1){
        println(element)
    }
    println("....mutableSet1.first()....")
    println(mutableSet1.first())

    println("...mutableSet1.indexOf(4)...")
    println(mutableSet1.indexOf(4))

    println("...mutableSet1.drop(3)...")
    println(mutableSet1.drop(3))
}

输出:

....mutableSet1....
2
6
4
29
5
....mutableSet1.first()....
2
...mutableSet1.indexOf(4)...
2
...mutableSet1.drop(3)...
[29, 5]