📅  最后修改于: 2021-01-05 07:52:15             🧑  作者: Mango
Kotlin MutableSet接口是元素的一般无序集合。 MutableSet接口不支持重复元素。该接口是可变的,因此其方法支持读写功能,支持添加和删除元素。
Set接口使用mutableSetOf()函数创建set接口的对象列表,其中包含元素列表。
interface MutableSet : Set, MutableCollection (source)
Properties | Description |
---|---|
abstract val size: Int | It returns the size of collection. |
Kotlin MutableSet接口具有多种功能。下面将介绍其某些功能。
Functions | Description |
---|---|
abstract fun add(element: E): Boolean | It adds the given element to the collection. |
abstract fun addAll(elements: Collection |
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 |
It removes all the elements from current collection which are given in collection. |
abstract fun retainAll(elements: Collection |
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 |
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 |
It returns true if collection contains at least one element. |
fun |
It returns true if at least element matches the given the given predicate. |
fun |
It returns a list which contains only distinct elements from the given collection. |
fun |
It returns a list which contains all elements except first n elements. |
fun |
It returns an element at given index or throw an IndexOutOfBoundException if given index is not present in collection. |
fun 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 |
It returns the largest element or null if there is no element in the collection. |
fun |
It returns the smallest element or null if there is no element in the collection. |
fun |
It removes the single specified element if it in present in current collection. |
fun elements: Collection ): Boolean |
It removes all the elements of current collection which are contained in specified collection. |
fun elements: Collection ): Boolean |
It retains all the elements in current collection which are contained in specified collection. |
fun |
It returns the elements in reversed order. |
让我们创建一个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接口不支持重复的元素。
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
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
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
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
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]