Here you can find the source of toString(List
public static <T> String toString(List<T> list)
//package com.java2s; //License from project: Apache License import java.util.List; public class Main { public static <T> String toString(List<T> list) { StringBuilder stringBuilder = new StringBuilder(); if (list.size() == 1) { stringBuilder.append(list.get(0)); } else if (list.size() == 2) { stringBuilder.append(list.get(0) + " and " + list.get(1)); } else if (list.size() > 0) { stringBuilder.append(list.get(0)); for (int i = 1; i < list.size() - 1; i++) { stringBuilder.append(", " + list.get(i)); }//from ww w .j a v a 2 s .co m stringBuilder.append(" and " + list.get(list.size() - 1)); } return stringBuilder.toString(); } public static <T> String toString(List<T> list, String delimiter) { String returnString = ""; if (list != null && list.size() > 0) { StringBuilder returnStringBuilder = new StringBuilder(); for (Object string : list) { returnStringBuilder.append(string + delimiter); } returnString = returnStringBuilder.substring(0, returnStringBuilder.length() - delimiter.length()); } return returnString; } }