📜  Kotlin集

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

Kotlin设定介面

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): Boolean 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 Iterable.all(predicate: (T) -> Boolean): Boolean It returns true if all the elements matches with given predicate.
fun Iterable.any(): Boolean It returns true if the collection contains at least one element.
fun Iterable.count(predicate: (T) -> Boolean): Int It returns the total number of elements matching with 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.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.filter(
predicate: (T) -> Boolean
): List
It returns a list which contains only those elements matches with given predicate.
fun Iterable.filterIndexed(
predicate: (index: Int, T) -> Boolean
): List
It returns a list which contains only those elements matches with given predicate.
fun Iterable.filterNot(
predicate: (T) -> Boolean
): List
It returns a list which contains only those elements which does not matches with given predicate.
fun Iterable.find(predicate: (T) -> Boolean): T? It returns the first element which matches with given predicate, or null if no such element was found.
fun Iterable.findLast(predicate: (T) -> Boolean): T? It returns the last element which matches with given predicate, or null if no such element was found.
fun Iterable.first(): T It returns the first element.
fun Iterable.first(predicate: (T) -> Boolean): T It returns the first element which matches the given predicate.
fun Iterable.firstOrnull(): T? It returns the first element or null if collection is empty.
fun Iterable.indexOf(element: T): Int It returns the first index of given element, or -1 if element does not contains in collection.
fun Iterable.indexOfFirst(
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 Iterable.indexOfLast(
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 Iterable.intersect(
other: Iterable
): Set
It returns a set which contains all elements present in both this set and given collection.
fun Collection.isNotEmpty(): Boolean It returns true if is not empty.
fun Iterable.last(): T It returns the last element.
fun Iterable.last(predicate: (T) -> Boolean): T It returns the last element which matches with given predicate.
fun Iterable.lastIndexOf(element: T): Int It returns the last index of given element, or -1 if element does not exist in collection.
fun Iterable.lastOrnull(): T? It returns the last element of collection, or null if collection is empty.
fun Iterable.lastOrnull(predicate: (T) -> Boolean): T? It returns the last element after matching the given predicate, or returns null if no such element found in collection.
fun > Iterable.max(): T? It returns the largest element or null if no elements in collection.
fun > Iterable.maxBy(
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 > Iterable.min(): T? It returns the smallest element or null if there is no element in the collection.
fun > Iterable.minBy(
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 Set.minus(element: T): Set It returns a set which contains all the elements of original set except those given element.
operator fun Set.minus(elements: Iterable): Set It returns a set which contains all the elements of original set except those given elements collection.
operator fun Iterable.minus(element: T): List It returns a list which contains all the elements of original collection except those contained in the given elements array.
fun Set.minusElement(element: T): Set It returns a set which contains all the elements of original set except those given element.
fun Iterable.minusElement(element: T): List It returns a list which contains all the elements of original collection except the first occurrence of the given element.
operator fun Set.plus(element: T): Set 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 Set.plus(elements: Iterable): Set 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 Iterable.plus(element: T): List It returns a list which contains all the elements of the original collection as well as the given element.
fun Set.plusElement(element: T): Set It returns a set which contains all the elements of the original set as well as the given element.
fun Iterable.plusElement(element: T): List It returns a list which contains all the elements of the original collection as well as the given element.
fun Iterable.reversed(): List It returns a list with elements in the reverse order.
fun Iterable.single(): T It returns the single element, or it throws an exception if the collection has more than one elements or empty.
fun Iterable.singleOrnull(): T? It returns a single element, or null if the collection has more than one element or it is empty.

Kotlin设置界面示例1

让我们创建一个使用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

在上面的示例中,我们在intSetmySet中两次声明了元素4,但是遍历它们时,它们仅将元素4print一次。这是因为set接口不支持重复元素。

Kotlin设置接口示例2-contains()和containsAll()

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

Kotlin设置接口示例3-isEmpty()和isNotEmpty()

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

Kotlin设置接口示例4-drop()

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

Kotlin设置接口示例5-elementAt()和elementAtOrNull()

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