Kotlin 集:setOf()
Kotlin Set 接口是一个通用的无序元素集合,它不包含重复的元素。 Kotlin 支持两种类型的集合可变和不可变。
setOf()是不可变的,意味着它只支持只读功能,而mutableSetOf()是可变的,意味着它支持读取和写入这两种功能。
句法:
fun setOf( vararg elements: T): Set
描述:
- 此函数返回一组新的只读给定元素。
- 元素被迭代,根据它们被存储。
setOf()函数的 Kotlin 程序:
Kotlin
fun main(args: Array)
{
//declaring a set of strings
val seta = setOf("Geeks" , "for", "geeks")
//declaring a set of characters
val setb = setOf( "G" , "f" , "g" )
//declaring a set of integers
val setc = setOf( 1 ,2 , 3 , 4 )
//traversing through a set of strings
for(item in seta)
print( item )
println()
//traversing through a set of characters
for(item in setb)
print( item )
println()
//traversing through a set of integers
for(item in setc)
print( "$item ")
}
Kotlin
fun main(args: Array) {
val captains = setOf("Kohli","Smith","Root","Malinga","Rohit","Dhawan")
println("The element at index 2 is: "+captains.elementAt(2))
println("The index of element is: "+captains.indexOf("Smith"))
println("The last index of element is: "+captains.lastIndexOf("Rohit"))
}
Kotlin
fun main(args: Array){
val captains = setOf(1,2,3,4,"Kohli","Smith",
"Root","Malinga","Rohit","Dhawan")
println("The first element of the set is: "+captains.first())
println("The last element of the set is: "+captains.last())
}
Kotlin
fun main(args: Array) {
val num = setOf(1 ,2, 3, 4, 5, 6, 7, 8)
println("The number of element in the set is: "+num.count())
println("The maximum element in the set is: "+num.max())
println("The minimum element in the set is: "+num.min())
println("The sum of the elements in the set is: "+num.sum())
println("The average of elements in the set is: "+num.average())
}
Kotlin
fun main(args: Array){
val captains = setOf(1,2,3,4,"Kohli","Smith",
"Root","Malinga","Rohit","Dhawan")
var name = "Dhawan"
println("The set contains the element $name or not?" +
" "+captains.contains(name))
var num = 5
println("The set contains the element $num or not?" +
" "+captains.contains(num))
println("The set contains the given elements or not?" +
" "+captains.containsAll(setOf(1,3,"Root")))
}
Kotlin
fun main(args: Array) {
//creating an empty set of strings
val seta = setOf()
//creating an empty set of integers
val setb =setOf()
//checking if set is empty or not
println("seta.isEmpty() is ${seta.isEmpty()}")
// Since Empty sets are equal
//checking if two sets are equal or not
println("seta == setb is ${seta == setb}")
println(seta) //printing first set
}
输出:
Geeksforgeeks
Gfg
1 2 3 4
设置索引 –
使用索引函数 indexOf() , lastIndexOf() 我们可以获得指定元素的索引。我们还可以使用 elementAt()函数找到某个特定索引处的元素。
Kotlin 使用索引的程序——
科特林
fun main(args: Array) {
val captains = setOf("Kohli","Smith","Root","Malinga","Rohit","Dhawan")
println("The element at index 2 is: "+captains.elementAt(2))
println("The index of element is: "+captains.indexOf("Smith"))
println("The last index of element is: "+captains.lastIndexOf("Rohit"))
}
输出:
The element at index 2 is: Root
The index of element is: 1
The last index of element is: 4
设置第一个和最后一个元素 -
我们可以使用 first() 和 last() 函数获取集合的第一个和元素。
Kotlin 程序 –
科特林
fun main(args: Array){
val captains = setOf(1,2,3,4,"Kohli","Smith",
"Root","Malinga","Rohit","Dhawan")
println("The first element of the set is: "+captains.first())
println("The last element of the set is: "+captains.last())
}
输出:
The first element of the set is: 1
The last element of the set is: Dhawan
设置基础 –
在这里,我们将讨论 count()、max()、min()、sum()、average() 等基本函数。
Kotlin 程序使用基本功能——
科特林
fun main(args: Array) {
val num = setOf(1 ,2, 3, 4, 5, 6, 7, 8)
println("The number of element in the set is: "+num.count())
println("The maximum element in the set is: "+num.max())
println("The minimum element in the set is: "+num.min())
println("The sum of the elements in the set is: "+num.sum())
println("The average of elements in the set is: "+num.average())
}
输出:
The number of element in the set is: 8
The maximum element in the set is: 8
The minimum element in the set is: 1
The sum of the elements in the set is: 36
The average of elements in the set is: 4.5
contains() 和 containsAll() 函数 –
这两种方法都用于检查集合中是否存在元素?
使用 contains() 和 containsAll()函数的 Kotlin 程序 -
科特林
fun main(args: Array){
val captains = setOf(1,2,3,4,"Kohli","Smith",
"Root","Malinga","Rohit","Dhawan")
var name = "Dhawan"
println("The set contains the element $name or not?" +
" "+captains.contains(name))
var num = 5
println("The set contains the element $num or not?" +
" "+captains.contains(num))
println("The set contains the given elements or not?" +
" "+captains.containsAll(setOf(1,3,"Root")))
}
输出:
The set contains the element Dhawan or not? true
The set contains the element 5 or not? false
The set contains the given elements or not? true
检查空集的相等性和使用 isEmpty() 函数 -
fun setOf(): Set
此语法返回特定类型的空集。
使用 isEmpty()函数的 Kotlin 程序 –
科特林
fun main(args: Array) {
//creating an empty set of strings
val seta = setOf()
//creating an empty set of integers
val setb =setOf()
//checking if set is empty or not
println("seta.isEmpty() is ${seta.isEmpty()}")
// Since Empty sets are equal
//checking if two sets are equal or not
println("seta == setb is ${seta == setb}")
println(seta) //printing first set
}
输出 :
seta.isEmpty() is true
seta == setb is true
[]