Scala Set take() method with example
Last Updated :
18 Oct, 2019
The take() method is utilized to return a set consisting of first ‘n’ elements of the set.
Method Definition: def take(n: Int): Set[A]
Where ‘n’ is specifies the number of element to select.
Return Type: It returns a set consisting of first ‘n’ elements of the set.
Example #1:
object GfG
{
def main(args : Array[String])
{
val s 1 = Set( 1 , 2 , 3 , 4 )
val result = s 1 .take( 2 )
println(result)
}
}
|
Example #2:
object GfG
{
def main(args : Array[String])
{
val s 1 = Set( 41 , 12 , 23 , 43 )
val result = s 1 .take( 3 )
println(result)
}
}
|
Share your thoughts in the comments
Please Login to comment...