Learn How To Use Packages And Data Structures In Scala

Data Structures

Data Structures

Packages are units that are used to organize code. Entities that can be contained in packages include classes, traits and objects. Using packages has several benefits. You prevent conflicts in naming and make it easy to locate and use components like classes and enumerations. This tutorial assumes you have set up a development environment in Eclipse. If you have not please refer to the setting up a development environment tutorial. Start eclipse and create a new project eduonix that will be used to demonstrate concepts. Click on file, new then scala project and give it a name.
new project

To define a package in Scala you use the keyword package that is then followed by name of the package. To package your code you create a Scala object (.scala) and place it there then store it in your preferred folder in project structure. The path to the folder where you store your Scala object becomes the name of the package. In the project created above create a scala object by clicking on file, new then Scala object. Give it a name and specify the folder where it will be stored. This folder will now become our package name.

In Scala there are two approaches to defining packages. You can use the Java like declaration or C# which relies on scope definition using curly braces. We will demonstrate the two approaches below. In Java like approach you begin with package name, declarations that are available at the package level then the code of the package. This is demonstrated below

package example

When you use the C# like approach you use curly braces to enforce scoping. The package above would now be created as shown below. Create a new scala object named clikePackage and add the code.


Package nesting is supported when writing Scala packages unlike Java which does not allow you to nest packages. To see how nesting happens look at the example below. Here the package spark is contained in package hadoop. When making a reference in class hql to scala class you use the short hand notation instead of using the long hand notation that begins at the base package.

To access the objects in packages you have created you use the import keyword. For example to access all objects in the Java like package we created above you add the objects in your code by importing as shown below. To import all objects you use an underscore

To import a specific a object you specify its name instead of using an underscore as shown below. Now you are able to use the coursePoints method directly without using a prefix.

Importing packages in Scala is not restrictive as it is in Java. Scala offers the flexibility to import packages anywhere, rename and show only some members and refer to objects and packages.

Whenever you create a .scala source some implicit imports happen that import all objects in scala package, Predef package and java.lang package so you are able to reference them directly. It is like manually performing the imports below

Standard java classes are contained in the java.lang package. Standard classes and objects in Scala are contained in scala package. The Predef package contains types, methods and conversions frequently used by Scala programs.

Scala provides an extensive choice of mutable and immutable collections. Immutable collections can not be changed while mutable collections can be changed through an update or extension. All collections are found in the scala.collections package. Mutable collections are in the mutable sub package while immutable objects are in the immutable sub package. The collections in Scala are very extensive to be covered in a few paragraphs. Here we will demonstrate a few commonly used collections and leave the reader to refer to documentation available at http://docs.scala-lang.org/overviews/collections/overview.html.

Start Scala in interactive mode by running scala at the command line. Before we can use any collection we need to perform relevant imports.

import pkg

Lists are one of the collections available in Scala. An example of how a list is created is shown below.
List(2, 8, 48, 34)

A set is a collection that can only contain unique values. An example is shown below. Observe from the output the duplicate value hadoop is dropped.


A tuple is used to represent a collection of items while avoiding the use of a class. An example is illustrated below.


Arrays are another collection available in Scala. Their use is shown below.


The different collection types do not give the same level of performance. They perform differently on operations such as insertions, prepending, updating and appending. This is what informs the decision on which collection type to use. A complete comparison of performance of collection types is available here .

This tutorial introduced you to packages which are components used in code organization. Definition of packages and where to store them was discussed. The two approaches for organizing packages were discussed. Package nesting was discussed. Importing code in packages so that it is available to other objects was discussed. Finally collections that are available were discussed.


Please enter your comment!
Please enter your name here