Saturday, September 27, 2008

Java vs. Scala - an example from the Google Collections API

As a Java developer, I've heard a lot of hubub, and some would say hype about Scala, so I decided to see what it has to offer. Scala is a multi-paradigm general purpose language, supporting both object-oriented and functional styles. As I learn Scala, I hope to also gain insight into the relative strengths and weaknesses of both languages. This is my first attempt at comparing the two.

Being somewhat familiar with the Google Collections API, I decided to do a simple side-by-side comparison between filtering a collection in Java (using Google collections) versus Scala. Below is the complete Java example, which takes a collection of integers from 1 to 10 and filters them down to only the even numbers:


package com.test;

import java.util.ArrayList;
import java.util.Collection;

import com.google.common.base.Join;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;

public class GoogleCollectFilter {

public static void main(String[] args) {
Collection<Integer> ints = new ArrayList<Integer>();
for( int i = 1; i <= 10; i++)
ints.add(new Integer(i));
Collection<Integer> filtered =
Collections2.filter(ints, new Predicate<Integer>() {

@Override
public boolean apply(Integer arg) {
return arg.intValue() % 2 == 0;
}});

System.out.println("(" + Join.join(", ", filtered) + ")" );
}
}


Here now is the equivalent code in Scala:


package com.test

object ScalaFilter {

def main( args : Array[String] ) = {
println((1 to 10).filter((n : Int) => n % 2 == 0))
}

}


If one were to judge purely by verbosity, Scala definitely wins this round. Even with Google's well-designed API, there are still many syntatic hoops that one must go through to do a simple filtering on a collection, due to current limitations in the Java language. Now I'm curious to find an example where the Java code is cleaner. Stay tuned...

5 comments:

Radu Floricica said...

It's not really fair. The scala code is pretty much what you would write in java if you wouldn't use collections. And it's painfully clear that using collections is not always beneficial.

I'm not saying scala is not better, just that the example may not have been fair.

On the other hand, the same code in clojure is:

(filter #'even? (range 10))

Julie said...

I do see your point. In Java, I could have easily written my main method as:

for( int 1 = 1; i <= 10; i++) {
if( i % 2 == 0 )
System.out.println(i + " ");
}

That is not what I was trying to convey though. I wanted to show a very simple example of anonymous functions in scala versus how to use the equivalent in Java as it stands now. Another comparison I'd like to make is with one of the Java closure proposals (BGGA, say, since it is most popular and now has a feature complete prototype).

Have you been studying the differences between Scala and Java? Have you made any interesting findings or drawn any conclusions? I'm personally doing this to sharpen my skill set, re-acclimate to functional programming, and align myself with the "next big thing" after Java. I may be right, and I may be wrong...time will tell.

Radu Floricica said...

No, I know nothing about either scala or google collections :) And I do get the point, better now after your reply: it's not as much a collection issue as it's an anonymous function one. That's probably java's biggest weakness right now. Nothing in either the language or (probably) the jvm is prepared to use higher-order functions.

I find it ironic javascript is right now a more powerfull language.

Unknown said...

I'm not sure if I like any of your examples. Both the Java code and the Scala code do not strike me as "beautiful code". The Java code looks too verbose, and could probably have been written somewhat cleaner.

The Scala code is very terse, but if you hadn't explained to me what it does I could not have figured that out.

Java is verbose, but the big advantage is that relative newbies can read it and see what's going on. I think that is a lot better than documentation (which programmers don't write).

I think each language has it's adavantages, and it's dangerous to do these comparisons. Learn both, and decide which you are going per project. Each problem has it's own solution.

Julie said...

rolfje - could you re-write the Java example to demonstrate a cleaner, more "beautiful" example of faking anonymous functions? I am always looking to improve my coding style.

Another point I am hearing is one of self-documenting code. I agree that code should be self-documenting whenever possible, but being less proficient in Scala, I don't yet have a sense of what self-documenting Scala code looks like.

I also agree on the point that one should choose the language that best fits the problem. This is exactly why I'm going through these exercises in my blog - I'm attempting to learn Scala and figure out why/when I'd want to use it over Java. Verbosity of code is certainly not a sole reason to use one language over another, but it was the first and most obvious metric I came across. Eventually I hope to develop a more comprehensive set of metrics that would help determine when one language would be better to use over the other. Far from being dangerous, I think that comparisons such as this, taken in the context of the problem at hand, are essential to making an informed language choice.