Java Reference
In-Depth Information
the Streams API provides (for example, there's support for zipping operations, which let you
combine elements of two lists), so you'll definitely gain a few programming idioms by checking it
out. These idioms may also make it into the Streams API in future versions of Java.
4 A list of notable and other packages can be found at
www.scala-lang.org/api/current/#package .
Finally, remember that in Java 8 you could ask for a pipeline to be executed in parallel by calling
parallel on a Stream. Scala has a similar trick; you only need to use the method par:
val linesLongUpper
= fileLines.par filter (_.length() > 10) map(_.toUpperCase())
Tuples
Let's now look at another feature that's often painfully verbose in Java: tuples . You may want to
use tuples to group people by their name and their phone number (here simple pairs) without
declaring an ad hoc new class and instantiate an object for it: (“Raoul”, “+ 44 007007007”),
(“Alan”, “+44 003133700”), and so on.
Unfortunately, Java doesn't provide support for tuples. So you have to create your own data
structure. Here's a simple Pair class:
public class Pair<X, Y> {
public final X x;
public final Y y;
public Pair(X x, Y y){
this.x = x;
this.y = y;
}
}
And of course you need to instantiate pairs explicitly:
Pair<String, String> raoul = new Pair<>("Raoul", "+ 44 007007007");
Pair<String, String> alan = new Pair<>("Alan", " +44 003133700");
Okay, but how about triplets? How about arbitrary-sized tuples? It becomes really tedious and
ultimately will affect the readability and maintenance of your programs.
 
Search WWH ::




Custom Search