Java List to String toString(List list)

Here you can find the source of toString(List list)

Description

to String

License

Apache License

Declaration

public static <T> String toString(List<T> list) 

Method Source Code

//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;
    }
}

Related

  1. toString(List list, String delimiter)
  2. toString(List list, String split)
  3. toString(List strings)
  4. toString(List strings)
  5. toString(List list)
  6. toString(List list)
  7. toStringClassList(List list)
  8. toStringDelimited(final Iterable list, final String delimiter)
  9. toStringItem(List> itemMapList, String subjectName)