Here you can find the source of implodeCommaAnd(List
public static String implodeCommaAnd(List<String> list, String comma, String and)
//package com.java2s; //License from project: Apache License import java.util.*; public class Main { public static String implodeCommaAnd(List<String> list, String comma, String and) { if (list.size() == 0) return ""; if (list.size() == 1) return list.get(0); String lastItem = list.get(list.size() - 1); String nextToLastItem = list.get(list.size() - 2); String merge = nextToLastItem + and + lastItem; list.set(list.size() - 2, merge); list.remove(list.size() - 1);//from w ww .j a va 2s . co m return implode(list, comma); } public static String implodeCommaAnd(List<String> list) { return implodeCommaAnd(list, ", ", " and "); } public static String implode(List<String> list, String glue) { StringBuilder ret = new StringBuilder(); for (int i = 0; i < list.size(); i++) { if (i != 0) { ret.append(glue); } ret.append(list.get(i)); } return ret.toString(); } }