Java - Write code to Join list of string with given delimiter to form string

Requirements

Write code to Join list of string with given delimiter to form string

Demo

//package com.book2s;
import java.util.List;

public class Main {
    public static void main(String[] argv) {
        List stringList = java.util.Arrays.asList("asdf", "book2s.com");
        String delimiter = "book2s.com";
        System.out.println(join(stringList, delimiter));
    }/*from ww w.j a va  2  s  . com*/

    /**
     * Join list of string with given delimiter to form string
     * @param stringList
     * @param delimiter
     * 
     * @return {@link String}
     */
    public static String join(List<String> stringList, String delimiter) {
        StringBuilder joinedString = new StringBuilder();

        if (stringList.size() > 0) {
            for (int i = 0; i < stringList.size(); i++) {
                joinedString.append(stringList.get(i).toString());
                if (i != stringList.size() - 1) {
                    joinedString.append(delimiter);
                }
            }
        }

        return joinedString.toString();

    }

    /**
     * join array of object with given delimiter to form string
     * 
     * @param iterable
     * @param delimiter
     * @return {@link String}
     */
    public static String join(Object[] iterable, String delimiter) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < iterable.length; i++) {
            builder.append(iterable[i]);
            if (i != iterable.length - 1) {
                builder.append(delimiter);
            }
        }
        return builder.toString();
    }
}

Related Exercise