Scala's standard library
Now that we have discussed OOP thoroughly, let's start writing classes and methods that do something useful. Part of the Scala installation is the Scala standard library, a big library of classes that is unique to Scala. We will discuss the following topics here:
- Generics
- Collections
- XML processing
Generics
Java used the ClassName<T>
notation for generics-aware classes. As we have seen, some classes, such as the Map<K, V>
interface, requires more types. For Map
, types need be specified for both the keys and the corresponding values that the map will hold.
In Scala, the ClassName[T]
notation is used instead:
val aList = List[Int](1, 2, 3, 5)
This creates an immutable list with five elements. Since List[Int]
is specified, adding an instance of any type other than Int
or an instance of a class that cannot be upcast to Int
is not allowed.
Similarly, when a class requires two types, it is written as:
val m = Map[String, String]("key1" -> "value1", "key2"...