Scala Collections – BitSet ”; Previous Next Bitset is a common base class for mutable and immutable bitsets. Bitsets are sets of non-negative integers and are represented as variable-size arrays of bits packed into 64-bit words. The memory footprint of a bitset is represented by the largest number stored in it. Declaring BitSet Variables The following is the syntax for declaring an BitSet variable. Syntax var z : BitSet = BitSet(0,1,2) Here, z is declared as an bit-set of non-negative integers which has three members. Values can be added by using commands like the following − Command var myList1: BitSet = myList + 3; Processing BitSet Below is an example program of showing how to create, initialize and process BitSet − Example import scala.collection.immutable.BitSet object Demo { def main(args: Array[String]) = { var mySet: BitSet = BitSet(0, 1, 2); // Add an element var mySet1: BitSet = mySet + 3; // Remove an element var mySet2: BitSet = mySet – 2; var mySet3: BitSet = BitSet(4, 5); // Adding sets var mySet4: BitSet = mySet1 ++ mySet3; println(mySet); println(mySet1); println(mySet2); println(mySet4); } } 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 BitSet(0, 1, 2) BitSet(0, 1, 2, 3) BitSet(0, 1) BitSet(0, 1, 2, 3, 4, 5) Print Page Previous Next Advertisements ”;
Category: scala Collections
Scala Collections – Home
Scala Collections Tutorial PDF Version Quick Guide Resources Job Search Discussion Scala has a rich set of collection library. Scala Collections may be strict or lazy. Lazy collections have elements that may not consume memory until they are accessed, like Ranges. Additionally, collections may be mutable (the contents of the reference can change) or immutable (the thing that a reference refers to is never changed). Note that immutable collections may contain mutable items. Audience This tutorial has been prepared for the beginners to help them understand Scala Collections library to use Collections in Scala based programs. Prerequisites For this tutorial, we assume the readers to have prior knowledge of basic software development using Java or Scala. It will help if you had some exposure to the software build and deployment process. Print Page Previous Next Advertisements ”;
Scala Collections – Environment Setup ”; Previous Next Scala can be installed on any UNIX flavored or Windows based system. Before you start installing Scala on your machine, you must have Java 1.8 or greater installed on your computer. Follow the steps given below to install Scala. Step 1: Verify Your Java Installation First of all, you need to have Java Software Development Kit (SDK) installed on your system. To verify this, execute any of the following two commands depending on the platform you are working on. If the Java installation has been done properly, then it will display the current version and specification of your Java installation. A sample output is given in the following table. Platform Command Sample Output Windows Open Command Console and type − >java -version Java version “1.8.0_31” Java (TM) SE Run Time Environment (build 1.8.0_31-b31) Java Hotspot (TM) 64-bit Server VM (build 25.31-b07, mixed mode) Linux Open Command terminal and type − $java -version Java version “1.8.0_31” Open JDK Runtime Environment (rhel-2.8.10.4.el6_4-x86_64) Open JDK 64-Bit Server VM (build 25.31-b07, mixed mode) We assume that the readers of this tutorial have Java SDK version 1.8.0_31 installed on their system. In case you do not have Java SDK, download its current version from https://www.oracle.com/technetwork/java/javase/downloads/index.html and install it. Step 2: Set Your Java Environment Set the environment variable JAVA_HOME to point to the base directory location where Java is installed on your machine. For example, Sr.No Platform & Description 1 Windows Set JAVA_HOME to C:ProgramFilesjavajdk1.8.0_31 2 Linux Export JAVA_HOME=/usr/local/java-current Append the full path of Java compiler location to the System Path. Sr.No Platform & Description 1 Windows Append the String “C:Program FilesJavajdk1.8.0_31bin” to the end of the system variable PATH. 2 Linux Export PATH=$PATH:$JAVA_HOME/bin/ Execute the command java -version from the command prompt as explained above. Step 3: Install Scala You can download Scala from www.scala-lang.org/downloads. At the time of writing this tutorial, I downloaded ”scala-2.13.1-installer.jar”. Make sure you have admin privilege to proceed. Now, execute the following command at the command prompt − Platform Command & Output Description Windows >java -jar scala-2.13.1-installer.jar> This command will display an installation wizard, which will guide you to install Scala on your windows machine. During installation, it will ask for license agreement, simply accept it and further it will ask a path where Scala will be installed. I selected default given path “C:Program FilesScala”, you can select a suitable path as per your convenience. Linux Command − $java -jar scala-2.13.1-installer.jar Output − Welcome to the installation of Scala 2.13.1! The homepage is at − http://Scala-lang.org/ press 1 to continue, 2 to quit, 3 to redisplay 1………………………………………… [ Starting to unpack ] [ Processing package: Software Package Installation (1/1) ] [ Unpacking finished ] [ Console installation done ] During installation, it will ask for license agreement, to accept it type 1 and it will ask a path where Scala will be installed. I entered /usr/local/share, you can select a suitable path as per your convenience. Finally, open a new command prompt and type Scala -version and press Enter. You should see the following − Platform Command Output Windows >scala -version Scala code runner version 2.13.1 — Copyright 2002-2019, LAMP/EPFL and Lightbend, Inc. Linux $scala -version Scala code runner version 2.13.1 — Copyright 2002-2019, LAMP/EPFL and Lightbend, Inc.tut Print Page Previous Next Advertisements ”;
Scala Collections – Vector
Scala Collections – Vector ”; Previous Next Scala Vector is a general purpose immutable data structure where elements can be accessed randomly. It is generally used for large collections of data. Declaring Vector Variables The following is the syntax for declaring an Vector variable. Syntax var z : Vector[String] = Vector(“Zara”,”Nuha”,”Ayan”) Here, z is declared as an vector of Strings which has three members. Values can be added by using commands like the following − Command var vector1: Vector[String] = z + “Naira”; Processing Vector Below is an example program of showing how to create, initialize and process Vector − Example import scala.collection.immutable.Vector object Demo { def main(args: Array[String]) = { var vector: Vector[String] = Vector(“Zara”,”Nuha”,”Ayan”); // Add an element var vector1: Vector[String] = vector :+ “Naira”; // Reverse an element var vector2: Vector[String] = vector.reverse; // sort a vector var vector3: Vector[String] = vector1.sorted; println(vector); println(vector1); println(vector2); println(vector3); } } 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 Vector(Zara, Nuha, Ayan) Vector(Zara, Nuha, Ayan, Naira) Vector(Ayan, Nuha, Zara) Vector(Ayan, Naira, Nuha, Zara) Print Page Previous Next Advertisements ”;
Scala Collections – Overview
Scala Collections – Overview ”; Previous Next Scala has a rich set of collection library. Collections are containers of things. Those containers can be sequenced, linear sets of items like List, Tuple, Option, Map, etc. The collections may have an arbitrary number of elements or be bounded to zero or one element (e.g., Option). Collections may be strict or lazy. Lazy collections have elements that may not consume memory until they are accessed, like Ranges. Additionally, collections may be mutable (the contents of the reference can change) or immutable (the thing that a reference refers to is never changed). Note that immutable collections may contain mutable items. For some problems, mutable collections work better, and for others, immutable collections work better. When in doubt, it is better to start with an immutable collection and change it later if you need mutable ones. This chapter throws light on the most commonly used collection types and most frequently used operations over those collections. Sr.No Collections with Description 1 Scala Lists Scala”s List[T] is a linked list of type T. 2 Scala Sets A set is a collection of pairwise different elements of the same type. 3 Scala Maps A Map is a collection of key/value pairs. Any value can be retrieved based on its key. 4 Scala Tuples Unlike an array or list, a tuple can hold objects with different types. 5 Scala Options Option[T] provides a container for zero or one element of a given type. 6 Scala Iterators An iterator is not a collection, but rather a way to access the elements of a collection one by one. Print Page Previous Next Advertisements ”;
Scala Collections – ListBuffer ”; Previous Next Scala provides a data structure, the ListBuffer, which is more efficient than List while adding/removing elements in a list. It provides methods to prepend, append elements to a list. Declaring ListBuffer Variables The following is the syntax for declaring an ListBuffer variable. Syntax var z = ListBuffer[String]() Here, z is declared as an list-buffer of Strings which is initially empty. Values can be added by using commands like the following − Command z += “Zara”; z += “Nuha”; z += “Ayan”; Processing ListBuffer Below is an example program of showing how to create, initialize and process ListBuffer − Example import scala.collection.mutable.ListBuffer object Demo { def main(args: Array[String]) = { var myList = ListBuffer(“Zara”,”Nuha”,”Ayan”) println(myList); // Add an element myList += “Welcome”; // Add two element myList += (“To”, “Tutorialspoint”); println(myList); // Remove an element myList -= “Welcome”; // print second element println(myList(1)); } } 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 ListBuffer(Zara, Nuha, Ayan) ListBuffer(Zara, Nuha, Ayan, Welcome, To, Tutorialspoint) Nuha Print Page Previous Next Advertisements ”;