Scala Collections – scan

Scala Collections – Scan Method ”; Previous Next scan() method is a member of TraversableLike trait, it is similar to fold method but is used to apply a operation on each elements of collection and return a collection. Syntax The following is the syntax of fold method. def scan[B >: A, That](z: B)(op: (B, B) ? B)(implicit cbf: CanBuildFrom[Repr, B, That]): That Here, scan method takes associative binary operator function as a parameter. This method returns the updated collection as result. It considers first input as initial value and second input as a function. Usage Below is an example program of showing how to use scan method − Example object Demo { def main(args: Array[String]) = { val list = List(1, 2, 3 ,4) //apply operation to create a running total of all elements of the list val list1 = list.scan(0)(_ + _) //print list println(list1) } } Here we”ve passed 0 as initial value to scan function and then all values are added. Save the above program in Demo.scala. The following commands are used to compile and execute this program. Command >scalac Demo.scala >scala Demo Output List(0, 1, 3, 6, 10) Print Page Previous Next Advertisements ”;

Scala Collections – zip

Scala Collections – Zip Method ”; Previous Next zip() method is a member of IterableLike trait, it is used to merge a collection to current collection and result is a collection of pair of tuple elements from both collections. Syntax The following is the syntax of zip method. def zip[B](that: GenIterable[B]): Iterable[(A, B)] Here, zip method takes a collection as parameter. This method returns the updated collection of pair as result. Usage Below is an example program of showing how to use zip method − Example object Demo { def main(args: Array[String]) = { val list = List(1, 2, 3 ,4) val list1 = List(“A”, “B”, “C”, “D”) //apply operation to create a zip of list val list2 = list zip list1 //print list println(list2) } } Save the above program in Demo.scala. The following commands are used to compile and execute this program. Command >scalac Demo.scala >scala Demo Output List((1,A), (2,B), (3,C), (4,D)) Print Page Previous Next Advertisements ”;

Scala Collections – HashSet

Scala Collections – HashSet ”; Previous Next Scala Set is a collection of pairwise different elements of the same type. In other words, a Set is a collection that contains no duplicate elements. HashSet implements immutable sets and uses hash table. Elements insertion order is not preserved. Declaring HashSet Variables The following is the syntax for declaring an HashSet variable. Syntax var z : HashSet[String] = HashSet(“Zara”,”Nuha”,”Ayan”) Here, z is declared as an hash-set of Strings which has three members. Values can be added by using commands like the following − Command var myList1: HashSet[String] = myList + “Naira”; Processing HashSet Below is an example program of showing how to create, initialize and process HashSet − Example import scala.collection.immutable.HashSet object Demo { def main(args: Array[String]) = { var mySet: HashSet[String] = HashSet(“Zara”,”Nuha”,”Ayan”); // Add an element var mySet1: HashSet[String] = mySet + “Naira”; // Remove an element var mySet2: HashSet[String] = mySet – “Nuha”; // Create empty set var mySet3: HashSet[String] = HashSet.empty[String]; println(mySet); println(mySet1); println(mySet2); println(mySet3); } } Save the above program in Demo.scala. The following commands are used to compile and execute this program. Command >scalac Demo.scala >scala Demo Output HashSet(Zara, Nuha, Ayan) HashSet(Zara, Nuha, Ayan, Naira) HashSet(Zara, Ayan) HashSet() Print Page Previous Next Advertisements ”;

Scala Collections – foldRight

Scala Collections – FoldRight Method ”; Previous Next foldRight() method is a member of TraversableOnce trait, it is used to collapse elements of collections. It navigates elements from Right to Left order. Syntax The following is the syntax of foldRight method. def foldRight[B](z: B)(op: (B, A) ? B): B Here, fold method takes associative binary operator function as a parameter. This method returns the resulted value. Usage Below is an example program of showing how to use foldRight method − Example object Demo { def main(args: Array[String]) = { val list = List(1, 2, 3 ,4) //apply operation to get sum of all elements of the list val result = list.foldRight(0)(_ + _) //print result println(result) } } Here we”ve passed 0 as initial value to foldRight function and then all values are added. Save the above program in Demo.scala. The following commands are used to compile and execute this program. Command >scalac Demo.scala >scala Demo Output 10 Print Page Previous Next Advertisements ”;

Scala Collections – map

Scala Collections – Map Method ”; Previous Next map() method is a member of TraversableLike trait, it is used to run a predicate method on each elements of a collection. It returns a new collection. Syntax The following is the syntax of map method. def map[B](f: (A) ? B): Traversable[B] Here, map method takes a prediate function as a parameter. This method returns the updated collection. Usage Below is an example program of showing how to use map method − Example object Demo { def main(args: Array[String]) = { val list = List(1, 2, 3 ,4) //apply operation to get twice of each element. val result = list.map(_ * 2) //print result println(result) } } Save the above program in Demo.scala. The following commands are used to compile and execute this program. Command >scalac Demo.scala >scala Demo Output List(2, 4, 6, 8) Print Page Previous Next Advertisements ”;

Scala Collections – Stack

Scala Collections – Stack ”; Previous Next Stack is Last In First Out, LIFO data structure and allows to insert and retrieve element at top, in LIFO manner. Declaring Stack Variables The following is the syntax for declaring an Stack variable. Syntax val stack = Stack(1, 2, 3, 4, 5) Here, stack is declared as an Stack of numbers. Value can be added at top by using commands like the following − Command stack.push(6) Value can be retrived from top by using commands like the following − Command stack.top Value can be removed from top by using commands like the following − Command stack.pop Processing Stack Below is an example program of showing how to create, initialize and process Stack − Example import scala.collection.mutable.Stack object Demo { def main(args: Array[String]) = { var stack: Stack[Int] = Stack(); // Add elements stack.push(1); stack.push(2); // Print element at top println(“Top Element: ” + stack.top) // Print element println(“Removed Element: ” + stack.pop()) // Print element println(“Top Element: ” + stack.top) } } Save the above program in Demo.scala. The following commands are used to compile and execute this program. Command >scalac Demo.scala >scala Demo Output Top Element: 2 Removed Element: 2 Top Element: 1 Print Page Previous Next Advertisements ”;

Scala Collections – foldLeft

Scala Collections – FoldLeft Method ”; Previous Next foldLeft() method is a member of TraversableOnce trait, it is used to collapse elements of collections. It navigates elements from Left to Right order. It is primarily used in recursive functions and prevents stack overflow exceptions. Syntax The following is the syntax of fold method. def foldLeft[B](z: B)(op: (B, A) ? B): B Here, foldLeft method takes associative binary operator function as a parameter. This method returns the result as value. Usage Below is an example program of showing how to use foldLeft method − Example object Demo { def main(args: Array[String]) = { val list = List(1, 2, 3 ,4) //apply operation to get sum of all elements of the list val result = list.foldLeft(0)(_ + _) //print result println(result) } } Here we”ve passed 0 as initial value to fold function and then all values are added. Save the above program in Demo.scala. The following commands are used to compile and execute this program. Command >scalac Demo.scala >scala Demo Output 10 Print Page Previous Next Advertisements ”;

Scala Collections – flatten

Scala Collections – Flatten Method ”; Previous Next flatten() method is a member GenericTraversableTemplate trait, it returns a single collection of elements by merging child collections. Syntax The following is the syntax of flatten method. def flatten[B]: Traversable[B] Here, f: (A) ? GenTraversableOnce[B] is a predicate or condition to be applied on each element of the collection. This method returns the Option element containing the matched element of iterator which satisfiles the given condition. Usage Below is an example program of showing how to use flatten method − Example object Demo { def main(args: Array[String]) = { val list = List(List(1,2), List(3,4)) //apply operation val result = list.flatten //print result println(result) } } Save the above program in Demo.scala. The following commands are used to compile and execute this program. Command >scalac Demo.scala >scala Demo Output List(1, 2, 3, 4) Print Page Previous Next Advertisements ”;

Scala Collections – Multi-Dimensional Array

Scala Collections – Multi-Dimensional Array ”; Previous Next There are many situations where you would need to define and use multi-dimensional arrays (i.e., arrays whose elements are arrays). For example, matrices and tables are examples of structures that can be realized as two-dimensional arrays. The following is the example of defining a two-dimensional array − var myMatrix = ofDim[Int](3,3) This is an array that has three elements each being an array of integers that has three elements. Try the following example program to process a multi-dimensional array − Example import Array._ object Demo { def main(args: Array[String]) { var myMatrix = ofDim[Int](3,3) // build a matrix for (i <- 0 to 2) { for ( j <- 0 to 2) { myMatrix(i)(j) = j; } } // Print two dimensional array for (i <- 0 to 2) { for ( j <- 0 to 2) { print(” ” &plus; myMatrix(i)(j)); } println(); } } } Save the above program in Demo.scala. The following commands are used to compile and execute this program. Command >scalac Demo.scala >scala Demo Output 0 1 2 0 1 2 0 1 2 Print Page Previous Next Advertisements ”;

Scala Collections – Useful Resources

Scala Collections – Useful Resources ”; Previous Next The following resources contain additional information on Scala. Please use them to get more in-depth knowledge on this topic. Useful Links on Scala The Scala Programming Language − All the information required to start writing programs in Scala! Contains manuals, a tour of Scala, information on the available Scala books, many guides, and useful documentation resources. Standard scaladoc Scala API − Browse the online API documentation generated by using our standard scaladoc tool. JavaTM 2 SDK, Standard Edition − Official site for JavaTM 2 SDK, Standard Edition. Free Java Download − Download Java for your desktop computer now! Sun Developer Network − Sun Microsystem”s official website listing down all the API documentation, latest Java Technologies, Books and other resource. Useful Books on Scala To enlist your site on this page, please drop an email to [email protected] Print Page Previous Next Advertisements ”;