Java Utililty Methods StringJoiner Usage

List of utility methods to do StringJoiner Usage

Description

The list of methods to do StringJoiner Usage are organized into topic(s).

Method

Stringcapitalise(String string)
Returns a new string consisting of the characters in the specified String, with the first letter of each word capitalised, and the rest lower-case.
String[] words = string.split(" ");
StringJoiner builder = new StringJoiner(" ");
for (String word : words) {
    String capitalised = Character.toUpperCase(word.charAt(0)) + word.substring(1).toLowerCase();
    builder.add(capitalised);
return builder.toString();
StringconvertListToString(String[] stringList)
Given a string list, gets the text from the list in the following manner: apple, pear, pineapple
if (stringList == null || stringList.length == 0) {
    return "";
StringJoiner stringJoiner = new StringJoiner(", ");
for (String string : stringList) {
    stringJoiner.add(string);
return stringJoiner.toString();
...
StringgenerateDeleteCommand(int deleteDisplayIndex)
Generates a delete command at the given displayed index.
StringJoiner commandJoiner = new StringJoiner(" ");
commandJoiner.add("delete").add(String.valueOf(deleteDisplayIndex));
return commandJoiner.toString();
StringgetTraceString(StackTraceElement[] stackTraceElements)
get Trace String
final StringJoiner sj = new StringJoiner(System.lineSeparator());
for (final StackTraceElement stackTraceElement : stackTraceElements) {
    sj.add(stackTraceElement.toString());
return sj.toString();
Stringimplode(T[] array, String delim)
Based on implode() in PHP
final StringJoiner sj = new StringJoiner(delim);
for (T o : array) {
    sj.add(o.toString());
return sj.toString();
StringimplodeString(Iterable strings, String delimiter)
(Based on implode() in PHP)
final StringJoiner sj = new StringJoiner(delimiter);
strings.forEach(sj::add);
return sj.toString();
ListincrementalComputationOfComponentWiseAverage(List average, int n, List newItems)
Incrementally calculates component-wise averages, given previously calculated averages (out of n numbers) and a list of new numbers.
if (average == null) {
    fatalError("Util.incrementalComputationOfComponentWiseAverage must receive a non-null List");
if (average.size() == 0) {
    for (int i = 0; i != newItems.size(); i++) {
        average.add(new Double(0));
for (int i = 0; i != newItems.size(); i++) {
    double currentAverage = ((Double) average.get(i)).doubleValue();
    double newItem = ((Double) newItems.get(i)).doubleValue();
    double newAverage = (currentAverage * n + newItem) / (n + 1);
    average.set(i, new Double(newAverage));
return average;
Stringjoin(Collection data, String delimiter)
join
return join(data, new StringJoiner(delimiter));
Stringjoin(final String left, final String right)
join
return new StringJoiner(".").add(left).add(right).toString();
Stringjoin(String separator, Collection c)
Returns a string formed by the concatenation of string versions of the elements in a collection, separated by a given separator.
Iterator it = c.iterator();
return join(separator, it);