Kotlin 简明教程
Kotlin - Sets
Kotlin 集合是对项目的无序集合。Kotlin 集合可以是可变的 ( mutableSetOf ) 或只读的 ( setOf )。Kotlin 可变或不可变集合不允许有重复的元素。
Kotlin set is an unordered collection of items. A Kotlin set can be either mutable (mutableSetOf) or read-only (setOf). Kotlin mutable or immutable sets do not allow to have duplicate elements.
Creating Kotlin Sets
对于集合创建,请对只读集合使用标准库函数 setOf() ,对可变集合使用 mutableSetOf() 。
For set creation, use the standard library functions setOf() for read-only sets and mutableSetOf() for mutable sets.
Example
fun main() {
val theSet = setOf("one", "two", "three", "four")
println(theSet)
val theMutableSet = mutableSetOf("one", "two", "three", "four")
println(theMutableSet)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[one, two, three, four]
[one, two, three, four]
Loop through Kotlin Sets
有很多方法可以遍历 Kotlin 集合。让我们一个个来研究它们:
There are various ways to loop through a Kotlin Set. Lets study them one by one:
Using toString() function
fun main() {
val theSet = setOf("one", "two", "three", "four")
println(theSet.toString())
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[one, two, three, four]
Using Iterator
fun main() {
val theSet = setOf("one", "two", "three", "four")
val itr = theSet.asIterable().iterator()
while (itr.hasNext()) {
println(itr.next())
}
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
one
two
three
four
Size of Kotlin Set
我们可以使用 size 属性获得集合中的元素总数:
We can use size property to get the total number of elements in a set:
fun main() {
val theSet = setOf("one", "two", null, "four", "five")
println("Size of the Set " + theSet.size)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
Size of the Set 5
The "in" Operator
in 运算符可用于检查集合中元素的存在。
The in operator can be used to check the existence of an element in a set.
The contain() Method
contain() 方法也可以用于检查集合中元素的存在。
The contain() method can also be used to check the existence of an element in a set.
The isEmpty() Method
isEmpty() 方法返回 true(不包含元素)如果集合为空,否则返回 false。
The isEmpty() method returns true if the collection is empty (contains no elements), false otherwise.
The indexOf() Method
indexOf() 方法返回集合中指定元素的第一次出现的位置,如果集合中不包含指定元素,则返回 -1。
The indexOf() method returns the index of the first occurrence of the specified element in the set, or -1 if the specified element is not contained in the set.
The elementAt() Method
elementAt() 方法可用于获得集合中指定位置的元素。
The elementAt() method can be used to get the element at the specified index in the set.
Set Addition
我们可以使用 + 运算符将两个或多个集合添加到一个集合中。这将把第二个集合添加到第一个集合中,丢弃重复元素。
We can use + operator to add two or more sets into a single set. This will add second set into first set, discarding the duplicate elements.
Example
fun main() {
val firstSet = setOf("one", "two", "three")
val secondSet = setOf("one", "four", "five", "six")
val resultSet = firstSet + secondSet
println(resultSet)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[one, two, three, four, five, six]
Set Subtraction
我们可以使用 - 运算符从另一个集合中减去一个集合。此操作会移除第一个集合中的公共元素并返回结果。
We can use - operator to subtract a set from another set. This operation will remove the common elements from the first set and will return the result.
Removing null a Set
我们可以使用 filterNotNull() 方法从集合中移除 null 元素。
We can use filterNotNull() method to remove null element from a set.
fun main() {
val theSet = setOf("one", "two", null, "four", "five")
val resultSet = theSet.filterNotNull()
println(resultSet)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[one, two, four, five]
Sorting Elements
我们可以使用 sorted() 方法按升序排列集合元素,或使用 sortedDescending() 方法按降序排列集合元素。
We can use sorted() method to sort the elements in ascending order, or sortedDescending() method to sort the set elements in descending order.
fun main() {
val theSet = setOf(10, 20, 30, 31, 40, 50, -1, 0)
var resultSet = theSet.sorted()
println(resultSet)
resultSet = theSet.sortedDescending()
println(resultSet)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[-1, 0, 10, 20, 30, 31, 40, 50]
[50, 40, 31, 30, 20, 10, 0, -1]
Filtering Elements
我们可以使用 filter() 方法过滤出与给定谓词匹配的元素。
We can use filter() method to filter out the elements matching with the given predicate.
fun main() {
val theSet = setOf(10, 20, 30, 31, 40, 50, -1, 0)
val resultSet = theSet.filter{ it > 30}
println(resultSet)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[31, 40, 50]
Dropping First N Elements
我们可以使用 drop() 方法从集合中丢弃前N个元素。
We can use drop() method to drop first N elements from the set.
fun main() {
val theSet = setOf(10, 20, 30, 31, 40, 50, -1, 0)
val resultSet = theSet.drop(3)
println(resultSet)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[31, 40, 50, -1, 0]
Grouping Set Elements
我们可以使用 groupBy() 方法对与给定谓词匹配的元素进行分组。
We can use groupBy() method to group the elements matching with the given predicate.
fun main() {
val theSet = setOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultSet = theSet.groupBy{ it % 3}
println(resultSet)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
{1=[10, 31, 40], 0=[12, 30, 9, -3, 0]}
Mapping Set Elements
我们可以使用 map() 方法使用提供的函数映射所有元素:
We can use map() method to map all elements using the provided function:.
fun main() {
val theSet = setOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultSet = theSet.map{ it / 3 }
println(resultSet)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[3, 4, 10, 10, 13, 3, -1, 0]
Chunking Set Elements
我们可以使用 chunked() 方法从集合中按给定大小创建块。基于集合中元素的总数,最后一个块可能不包含等于块大小数量的元素。
We can use chunked() method to create chunks of the given size from a set. Last chunk may not have the elements equal to the number of chunk size based on the total number of elements in the set.
fun main() {
val theSet = setOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultSet = theSet.chunked(3)
println(resultSet)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[[10, 12, 30], [31, 40, 9], [-3, 0]]
Windowing Set Elements
我们可以使用 windowed() 方法对一组元素范围使用给定大小的滑动窗口对一组元素进行移动,从而将其收集到一起。
We can use windowed() method to a set of element ranges by moving a sliding window of a given size over a collection of elements.
fun main() {
val theSet = setOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultSet = theSet.windowed(3)
println(resultSet)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[[10, 12, 30], [12, 30, 31], [30, 31, 40], [31, 40, 9], [40, 9, -3], [9, -3, 0]]
默认情况下,滑动窗口每次移动一步,但我们可以通过传递自定义步长值来更改此设置:
By default, the sliding window moves one step further each time but we can change that by passing a custom step value:
fun main() {
val theSet = setOf(10, 12, 30, 31, 40, 9, -3, 0)
val resultSet = theSet.windowed(3, 3)
println(resultSet)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[[10, 12, 30], [31, 40, 9]]
Kotlin mutable Set
我们可以使用 mutableSetOf() 创建可变集合,之后我们可以使用 add() 在同一集合中添加更多元素,并且我们可以使用 remove() 方法从集合中移除元素。
We can create mutable set using mutableSetOf(), later we can use add() to add more elements in the same set, and we can use remove() method to remove the elements from the set.
fun main() {
val theSet = mutableSetOf(10, 20, 30)
theSet.add(40)
theSet.add(50)
println(theSet)
theSet.remove(10)
theSet.remove(30)
println(theSet)
}
当你运行上述 Kotlin 程序时,它将生成以下输出:
When you run the above Kotlin program, it will generate the following output:
[10, 20, 30, 40, 50]
[20, 40, 50]