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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
println a.join(' ') |
When I was still working in Java 7, I wrote something like the following code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void printIntArray(int[] a) { | |
StringBuilder sb = new StringBuilder(); | |
boolean first = true; | |
for (int i = 0; i < a.length; i++) { | |
if (first) { | |
first = false; | |
} else { | |
sb.append(" "); | |
} | |
sb.append(Integer.toString(a[i])); | |
} | |
System.out.println(sb.toString()); | |
} |
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void printIntArray(int[] a) { | |
System.out.println(String.join(" ", Arrays.stream(a) | |
.mapToObj(String::valueOf) | |
.collect(Collectors.toList()))); | |
} |
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void printIntArray(int[] a) { | |
System.out.println(Arrays.stream(a) | |
.mapToObj(String::valueOf) | |
.collect(Collectors.joining(" "))); | |
} |
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.
No comments:
Post a Comment