Java Reference
In-Depth Information
Scala provides tuple literals , which means you can create tuples through simple syntactic
sugar—just the normal mathematical notation:
val raoul = ("Raoul", "+ 44 887007007")
val alan = ("Alan", " +44 883133700")
Scala supports arbitrary-sized [ 5 ] tuples, so the following are all possible:
5 Tuples have a limitation of 23 elements maximum.
You can access the elements of the tuples by their positions using the accessors _1, _2 (starting
at 1), for example:
Isn't that much nicer than what you'd have to write in Java? The good news is that there are
discussions about introducing tuple literals in future versions of Java (see chapter 16 for more
discussion of this).
Stream
The collections we described so far, List, Set, Map, and Tuple, are all evaluated eagerly (that is,
immediately). Of course by now you know that streams in Java 8 are evaluated on demand (that
is, lazily). You saw in chapter 5 that because of this property streams can represent an infinite
sequence without overflowing the memory.
Scala provides a corresponding lazily evaluated data structure called Stream too! But Streams in
Scala provide more features than those in Java. Streams in Scala remember values that were
computed so previous elements can be accessed. In addition, Streams are indexed so elements
can be accessed by an index just like a list. Note that the trade-off for these additional properties
is that Streams are less memory-efficient compared to Java 8's streams, because being able to
refer back to previous elements means the elements need to be “remembered” (cached).
 
Search WWH ::




Custom Search