Search This Blog

Wednesday, September 19, 2018

Java 8 offers static methods join as part of the String class definition.

The join method offers the convenience of not adding the delimiter at the end of the string as well. As a matter of fact, class String offers two join methods with the following signatures:

public static String join(CharSequence delimiter, CharSequence... elements)

public static String join(CharSequence delimiter, Iterable<? extends CharSequence> elements)

The method's contract doesn't allow the parameters to be passed as null. If the contract is violated, i.e. if one or both the parameters are null, then the method will throw a NullPointerException.

Example:

String.join("; ", myList);