Back around November, I started working problems at Hackerrank. One of my goals was to learn and get better at Java 8.
Frequently in these problems I had a need to print out an array of integers.
Before we get started in Java, the programming language I've mostly used at work for the last 3 years or so is Groovy, doing application development in Grails, writing Jenkins jobs, and just for general scripting. Here's how you print an array of integers (int[] a) in Groovy:
It's super-easy and concise.
When I was still working in Java 7, I wrote something like the following code:
Hackerrank sometimes uses huge arrays, like 50K or 200K, so I'm efficiently using StringBuilder. But the whole thing is super-verbose. Java 7 lacks anything like the join operator so I'm reduced to implementing it myself, more or less. There's five lines of code here devoted to adding a space before every character but the first. The Groovy solution is so concise, it hardly makes sense to write a method for it, but here it would be crazy not to.
Java 8 String.join() finally gives us a string join method like the one I was using in Perl over 20 years ago. Cool! Here's the way I was doing this in Java 8 for a while:
That's not bad. It's a big improvement over Java 7. It's actually a one-liner, though the line is kind of a long one. It's basically doing the same thing as the Groovy code, except I need to explicitly shepherd the output through a series of type conversions.
There's still a way to make this a little bit better, though. In order to come up with the second argument to String.join(), I'm converting my array to a stream, then doing another conversion to get a Collection. It turns out that the Java 8 team provided a reduce method Collectors.joining() which eliminates the need for String.join(). This makes it possible to do everything in a stream instead of the awkward series of conversions I had before.
And it's a bit more concise, too.
It might be tempting to look at this one example and say 'Groovy wins.' Well, after all, Groovy is built on top of Java such that the new features of Java 8 are available in Groovy, so, yeah, Groovy does win 😎.
My initial reaction to Java 8 lambdas was 'oh, Java is finally catching up to something Groovy has had since, what, 2007?' But it's important to understand that streams and the implementation of the filter-map-reduce abstraction implemented by Java 8 is a new thing that didn't exist in Groovy before. In recent years we've seen a lot of interest in languages like Scala and Clojure which support a functional style of programming in which the creation of mutable state is avoided for the sake of concurrency. This is what the streams feature of Java 8 is all about, parallelism.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment