Scala mkString in Java 8

With the release of JDK 8 M7 on June 13, 2013, the upcoming Java 8 release is now feature complete. Even though its final release is roughly 9 months from now, I wanted to give some of its new features a try. I believe Java v8 will be the most significant release since Java v5 with its support for closures via Project Lambda.

List to String

View source code for demo

Java 8's most important feature is the addition of lambda expressions (closures) and supporting features, which include method references, enhanced type inference, and virtual extension methods.

One simple use of a closure is the internal iteration of a collection into a String with delimiters, similar to Scala's standard mkString method.

import java.util.*;
import java.util.function.*;
import java.util.stream.*;

/**
 Demonstrates various Scala-like mkString in Java 8 using JDK8.
*/
public class MakeString {

  public static void main(String[] args) throws Exception {
    List<Person> persons = Arrays.asList(new Person("Joe", "Lauer"), new Person("John", "Doe"), new Person("George", "Washington"));

    // single line closures can omit some syntax
    String firstThenLast = ListUtils.mkString(persons, p -> p.getFirstName() + " " + p.getLastName(), "; ");
    System.out.println("first then last: " + firstThenLast);

    // includes optional syntax
    String lastThenFirst = ListUtils.mkString(persons, (p) -> { return p.getLastName() + ", " + p.getFirstName(); }, "; ");
    System.out.println("last then first: " + lastThenFirst);

    // same result but using only java 8 std libs
    String firstThenLast2 = persons
	.stream()
	.map(p -> p.getFirstName() + " " + p.getLastName())
	.collect(Collectors.toStringJoiner("; "))
        .toString();
    System.out.println("first then last v2: " + firstThenLast2);

    // filter out first names of "John"
    String noJohns = persons
	.stream()
	.filter(p -> !p.getFirstName().equalsIgnoreCase("John"))
	.map(p -> p.getFirstName() + " " + p.getLastName())
	.collect(Collectors.toStringJoiner("; "))
        .toString();
    System.out.println("no johns: " + noJohns);
  }

  public static class ListUtils {
    static public <E> String mkString(List<E> list, Function<E,String> stringify, String delimiter) {
      int i = 0;
      StringBuilder s = new StringBuilder();
      for (E e : list) {
        if (i != 0) { s.append(delimiter); }
	s.append(stringify.apply(e));
	i++;
      }
      return s.toString();
    }
  }

  public static class Person {
    private String firstName;
    private String lastName;

    public Person(String f, String l) {
      this.firstName = f;
      this.lastName = l;
    }

    public String getFirstName() {
	return firstName;
    }

    public String getLastName() {
	return lastName;
    }
  }
}

Tags

Archives