StringBuilder (StringBuilder builder, List<String> s, String glue, int N, String nthGlue) { int i = 1; for (Iterator<String> iterator = s.iterator(); iterator.hasNext(); i++) { String str = iterator.next(); builder.append(str); if (iterator.hasNext()) { if (i % N == 0) { builder.append(nthGlue); } else { builder.append(glue); } } } return builder; }