📅  最后修改于: 2021-01-05 07:51:07             🧑  作者: Mango
Kotlin Set接口是元素的一般无序集合。 Set接口不支持重复元素。该接口本质上是不可变的,其方法支持集合的只读功能。
Set接口使用setOf()函数创建set接口的对象列表,其中包含元素列表。
interface Set : Collection (source)
Properties | Description |
---|---|
abstract val size: Int | It returns the size of collection. |
Kotlin Set界面具有多种功能。下面将介绍其某些功能。
Functions | Description |
---|---|
abstract fun contains(element: E): Boolean | It checks the mention element is present in this collection. If it contains element, it returns true else returns false. |
abstract fun containsAll(elements: Collection |
It checks all the mention elements of specified collection are present in this collection. If it contains element, it returns true else returns false. |
abstract fun isEmpty(): Boolean | It returns true if the collection is empty (contains no elements) otherwise it returns false. |
abstract fun iterator(): Iterator |
It returns an iterator over the elements of set object. |
fun |
It returns true if all the elements matches with given predicate. |
fun |
It returns true if the collection contains at least one element. |
fun |
It returns the total number of elements matching with 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 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 predicate: (T) -> Boolean ): List |
It returns a list which contains only those elements matches with given predicate. |
fun predicate: (index: Int, T) -> Boolean ): List |
It returns a list which contains only those elements matches with given predicate. |
fun predicate: (T) -> Boolean ): List |
It returns a list which contains only those elements which does not matches with given predicate. |
fun |
It returns the first element which matches with given predicate, or null if no such element was found. |
fun |
It returns the last element which matches with given predicate, or null if no such element was found. |
fun |
It returns the first element. |
fun |
It returns the first element which matches the given predicate. |
fun |
It returns the first element or null if collection is empty. |
fun |
It returns the first index of given element, or -1 if element does not contains in collection. |
fun predicate: (T) -> Boolean ): Int |
It returns the index of first element which matches the given predicate, or -1 if the element does not contains in collection. |
fun predicate: (T) -> Boolean ): Int |
It returns the index of last element which matches the given predicate, or -1 if the element does not contains in collection. |
infix fun other: Iterable ): Set |
It returns a set which contains all elements present in both this set and given collection. |
fun |
It returns true if is not empty. |
fun |
It returns the last element. |
fun |
It returns the last element which matches with given predicate. |
fun |
It returns the last index of given element, or -1 if element does not exist in collection. |
fun |
It returns the last element of collection, or null if collection is empty. |
fun |
It returns the last element after matching the given predicate, or returns null if no such element found in collection. |
fun |
It returns the largest element or null if no elements in collection. |
fun selector: (T) -> R ): T? |
It returns the first element yielding the largest value of the given function, or it returns null if there are no elements in collection. |
fun |
It returns the smallest element or null if there is no element in the collection. |
fun selector: (T) -> R ): T? |
It returns the first element which gives the smallest value of the given function or null if there are no elements. |
operator fun |
It returns a set which contains all the elements of original set except those given element. |
operator fun |
It returns a set which contains all the elements of original set except those given elements collection. |
operator fun |
It returns a list which contains all the elements of original collection except those contained in the given elements array. |
fun |
It returns a set which contains all the elements of original set except those given element. |
fun |
It returns a list which contains all the elements of original collection except the first occurrence of the given element. |
operator fun |
It returns a set of all elements of original set as well as the given element if it is not already present in the set. |
operator fun |
It returns a set which contains all the elements of original set as well as the given elements collection which are not already present in the set. The returned set preserves the iteration of element in the same order of the original set. |
operator fun |
It returns a list which contains all the elements of the original collection as well as the given element. |
fun |
It returns a set which contains all the elements of the original set as well as the given element. |
fun |
It returns a list which contains all the elements of the original collection as well as the given element. |
fun |
It returns a list with elements in the reverse order. |
fun |
It returns the single element, or it throws an exception if the collection has more than one elements or empty. |
fun |
It returns a single element, or null if the collection has more than one element or it is empty. |
让我们创建一个使用setOf()函数声明和遍历set元素的示例。在此示例中,我们创建了一组非通用的Int类型和另一个Any类型的通用类型。
fun main(args: Array){
val intSet = setOf(2,6,4,29,4,5)
val mySet: Set = setOf(2,6,4,29,4,5,"Ashu","Ajay")
println(".......print Int set.........")
for(element in intSet){
println(element)
}
println(".......print Any set.........")
for(element in mySet){
println(element)
}
}
输出:
.......print Int set.........
2
6
4
29
5
.......print Any set.........
2
6
4
29
5
Ashu
Ajay
在上面的示例中,我们在intSet和mySet中两次声明了元素4,但是遍历它们时,它们仅将元素4print一次。这是因为set接口不支持重复元素。
contains()函数检查给定元素是否存在于当前集中。如果它包含在集合中,则该集合返回true,否则返回false。而containsAll()函数检查集合类型的所有元素是否存在于当前集合中。如果集合包含集合类型的所有元素,则返回true,否则返回false。
fun main(args: Array){
val mySet: Set = setOf(2,6,4,29,5,"Ashu","Ajay")
val intSet = setOf(6,4,29)
println(".......print Any set.........")
for(element in mySet){
println(element)
}
println("...mySet.contains\"Ashu\"...")
println(mySet.contains("Ashu"))
println("...mySet.contains(20)...")
println(mySet.contains(20))
println("...mySet.containsAll(intSet)...")
println(mySet.containsAll(intSet))
}
输出:
.......print Any set.........
2
6
4
29
5
Ashu
Ajay
...mySet.contains"Ashu"...
true
...mySet.contains(20)...
false
...mySet.containsAll(intSet)...
true
isEmpty()函数检查当前集合是否为空。如果集合为空,则isEmpty()函数返回true,否则返回false。并且isNotEmpty()检查当前集合是否为空。如果集合不为空,则isNotEmpty()函数返回true,否则返回false。
fun main(args: Array){
val mySet: Set = setOf(2,6,4,29,5,"Ashu","Ajay")
println(".......print Any set.........")
for(element in mySet){
println(element)
}
println("...mySet.isEmpty()...")
println(mySet.isEmpty())
println("...mySet.isNotEmpty()...")
println(mySet.isNotEmpty())
}
输出:
.......print Any set.........
2
6
4
29
5
Ashu
Ajay
...mySet.isEmpty()...
false
...mySet.isNotEmpty()...
true
drop()函数返回除集合的前n个元素以外的所有元素。
fun main(args: Array){
val mySet: Set = setOf(2,6,4,29,4,5,"Ajay","Ashu","Ajay")
println(".......print Any set.........")
for(element in mySet){
println(element)
}
val remainList= mySet.drop(4)
println(".......print Set after mySet.drop(4).........")
for(element in remainList){
println(element)
}
}
输出:
.......print Any set.........
2
6
4
29
5
Ajay
Ashu
.......print Set after mySet.drop(4).........
5
Ajay
Ashu
elementAt()函数返回给定索引处的元素,elementAtOrNull()函数也返回给定索引处的元素,但是如果指定的索引不包含element,则返回null。
fun main(args: Array){
val mySet: Set = setOf(2,6,4,29,4,5,"Ajay","Ashu","Ajay")
println(".......print Any set.........")
for(element in mySet){
println(element)
}
println(".......print mySet.elementAt(3).........")
println(mySet.elementAt(3))
println(".......print mySet.elementAtOrNull(5).........")
println(mySet.elementAtOrNull(5))
}
输出:
.......print Any set.........
2
6
4
29
5
Ajay
Ashu
.......print mySet.elementAt(3).........
29
.......print mySet.elementAtOrNull(5).........
Ajay