String Joiner example

Syntactically similar to Guava joiner, Java 8 introduced the long overdue StringJoiner and String.join(...) which allows the ability to construct a sequence of characters separated by a delimiter and optionally starting with a supplied prefix and ending with a supplied suffix. Just about every developer has ran into a situation where joining strings is required and many either have written their own, used guava joiner or apache's StringUtil.join. Finally, finally, finally it is baked into the standard API.

Join strings

@Test
public void join_strings() {

    StringJoiner sj = new StringJoiner(",");

    String joinedStrings = sj.add("test").add("whatever").toString();

    assertEquals("test,whatever", joinedStrings);
}

Join strings prefix & suffix

@Test
public void join_strings_prefix_suffix () {
    
    StringJoiner sj = new StringJoiner(",", "[", "]");

    String joinedStrings = sj.add("test").add("whatever").toString();

    assertEquals("[test,whatever]", joinedStrings);
}

Join a list of strings

@Test
public void join_a_list_of_strings() {

    List<String> cloudGroups = new ArrayList<>();
    cloudGroups.add("Cirrus");
    cloudGroups.add("Alto");
    cloudGroups.add("Stratus");
    cloudGroups.add("Vertical Growth");
    cloudGroups.add("Special Clouds");

    String cloudGroupsJoined = String.join(",", cloudGroups);

    assertEquals("Cirrus,Alto,Stratus,Vertical Growth,Special Clouds",
            cloudGroupsJoined);
}

Join list skipping null

@Test
public void join_a_list_of_strings_skipping_null() {

    List<String> cloudGroups = new ArrayList<>();
    cloudGroups.add("Cirrus");
    cloudGroups.add("Alto");
    cloudGroups.add(null);
    cloudGroups.add(null);
    cloudGroups.add("Stratus");
    cloudGroups.add("Vertical Growth");
    cloudGroups.add("Special Clouds");
    cloudGroups.add(null);

    String cloudGroupsJoined = cloudGroups.stream()
            .filter(Objects::nonNull).collect(Collectors.joining(","));

    assertEquals("Cirrus,Alto,Stratus," + "Vertical Growth,Special Clouds",
            cloudGroupsJoined);
}

Join list replacing null

@Test
public void join_a_list_of_strings_replacing_null() {

    List<String> cloudGroups = new ArrayList<>();
    cloudGroups.add("Cirrus");
    cloudGroups.add("Alto");
    cloudGroups.add(null);
    cloudGroups.add(null);
    cloudGroups.add("Stratus");
    cloudGroups.add("Vertical Growth");
    cloudGroups.add("Special Clouds");
    cloudGroups.add(null);

    Function<String, String> replaceNull = new Function<String, String>() {
        @Override
        public String apply(String t) {
            if (t == null) {
                return "[unknown]";
            } else {
                return t;
            }
        }
    };
    
    String cloudGroupsJoined = cloudGroups.stream()
//             .map(s -> { if (s == null) return "[unknown]"; else return s;})
//             TODO determine why cast isn't working quite right
            .map(replaceNull)
            .collect(Collectors.joining(" and "));

    assertEquals("Cirrus and Alto and [unknown] and [unknown] "
            + "and Stratus and Vertical Growth "
            + "and Special Clouds and [unknown]", cloudGroupsJoined);
}