Scala Set head() 方法与示例
head()方法用于显示集合的第一个元素。
Method Definition: def head: A
Return Type: It returns the first element of the set.
示例 #1:
// Scala program of head()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a set
val s1 = Set(5, 1, 2, 3, 4)
// Applying head method
val result = s1.head
// Display output
println(result)
}
}
输出:
5
示例 #2:
// Scala program of head()
// method
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating a set
val s1 = Set(41, 16, 22, 3, 51)
// Applying head method
val result = s1.head
// Display output
println(result)
}
}
输出:
41