Scala Set take() method with example Last Updated : 18 Oct, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: Scala // Scala program of take() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a set val s1 = Set(1, 2, 3, 4) // Applying take method val result = s1.take(2) // Display output println(result) } } Output: Set(5, 1) Example #2: Scala // Scala program of take() // method // Creating object object GfG { // Main method def main(args:Array[String]) { // Creating a set val s1 = Set(41, 12, 23, 43) // Applying take method val result = s1.take(3) // Display output println(result) } } Output: Set(41, 12, 23) Comment More infoAdvertise with us Next Article Scala Set take() method with example R rupesh_rao Follow Improve Article Tags : Scala Scala Scala-Method scala-collection Scala-Set +1 More Similar Reads Scala Stack take() method with example In Scala Stack class, the take() method is utilized to return a stack consisting of the first ânâ elements of the stack. Method Definition: def take(n: Int): Stack[A] Return Type: It returns a stack consisting of the first ânâ elements of the stack. Example #1: Scala // Scala program of take() // me 1 min read Scala Set takeRight() method with example The takeRight() method is utilized to return a set consisting of last 'n' elements of the set. Method Definition: def takeRight(n: Int):Set[A] Where 'n' is specifies the number of elements to select. Return Type: It returns a set consisting of last 'n' elements of the set. Example #1: Scala // Scala 1 min read Scala Set toMap() method with example The toMap() method is utilized to return a map consisting of all the elements of the set. Method Definition: def toMap[T, U]: Map[T, U] Return Type: It returns a map consisting of all the elements of the set. Example #1: Scala // Scala program of toMap() // method // Creating object object GfG { // 1 min read Scala SortedSet take() method with example The take() method is utilized to return a SortedSet consisting of first 'n' elements of the SortedSet. Method Definition: def take(n: Int): SortedSet[A] Where 'n' is specifies the number of element to select. Return Type: It returns a SortedSet consisting of first 'n' elements of the SortedSet. Exam 1 min read Scala Set -() method with example The -() method is utilized to creates a new set with a given element removed from the set. Method Definition: def -(elem: A): Set[A] Return Type: It returns a new set with a given element removed from the set. Example #1: Scala // Scala program of -() // method // Creating object object GfG { // Mai 1 min read Like