18 March 2017

I was looking for cleaner ways to filter a string in Java 8. I figured this would be easy as Java has lambdas nicely woven into the APIs, and String instances are already immutable so it seemed like a home-run.

I knew how to do it in a trivial Python example:

#!/usr/bin/env python
print ''.join([a for a in '123 a string !!!' if a.isalpha()])

I asked on Twitter:

My initial attempt was valiant, but very complicated. A Java String returns an IntStream from the chars() method, so at some point there has to be a conversion to a Character - this means boxing and unboxing, somewhere.

Others replied suggesting that I use

..a regular expression:

..or Kotlin:

..or the replace method:

All of these are good ideas, but not quite what I wanted.

I was looking for trivial amounts of conceptual noise - something I could stare at and process, quickly. Maybe Python was the right thing, after all? So I tried embedding Jython (the Python language implementation on the JVM). This required a pesky, um, let's call it utility library (the entire Python language and runtime) whose Maven coordinates are org.python : jython : 2.7.1.b3 :

import org.python.util.PythonInterpreter;
import java.util.Properties;

public class JythonApplication {
	public static void main(String[] args) {
		Properties properties = new Properties();
		properties.put("python.import.site", "false");
		PythonInterpreter.initialize(System.getProperties(), properties, new String[0]);
		System.out.println(new PythonInterpreter().eval("''.join([a for a in '123 a string !!!' if a.isalpha()])"));

Not bad! (I'm only kidding. Half kidding, anyway.) Some people liked it, alright? Jeez! :D

The following example does a bit less gymnastics than my previous example, and is functional, so I suppose it's the best solution so far. I learned a lot from this example, too.