Java List to String listToString(List list)

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

Description

list To String

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 *
 *   Copyright (c) 2008 Fujitsu Services Ltd.
 *
 *   Author: Nick Battle/*from  w w w .jav  a  2 s  .  co  m*/
 *
 *   This file is part of VDMJ.
 *
 *   VDMJ is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   VDMJ is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with VDMJ.  If not, see <http://www.gnu.org/licenses/>.
 *
 ******************************************************************************/

import java.util.List;

public class Main {
    public static <T> String listToString(List<T> list) {
        return listToString("", list, ", ", "");
    }

    public static <T> String listToString(List<T> list, String separator) {
        return listToString("", list, separator, "");
    }

    public static <T> String listToString(String before, List<T> list, String separator, String after) {
        StringBuilder sb = new StringBuilder();
        sb.append(before);

        if (!list.isEmpty()) {
            sb.append(list.get(0).toString());

            for (int i = 1; i < list.size(); i++) {
                sb.append(separator);
                sb.append(list.get(i).toString());
            }
        }

        sb.append(after);
        return sb.toString();
    }

    public static <T> String listToString(String separator, List<T> list) {
        return listToString("", separator, list, "");
    }

    public static <T> String listToString(String before, String separator, List<T> list, String after) {
        StringBuilder sb = new StringBuilder();
        sb.append(before);

        if (!list.isEmpty()) {
            sb.append(separator);
            sb.append(list.get(0).toString());

            for (int i = 1; i < list.size(); i++) {
                sb.append(separator);
                sb.append(list.get(i).toString());
            }
        }

        sb.append(after);
        return sb.toString();
    }
}

Related

  1. listToString(List list)
  2. listToString(List elements)
  3. listToString(List lines)
  4. listToString(List list)
  5. listToString(List list)
  6. listToString(List list, String prefix, String delimiter, String suffix)
  7. listToString(List list, String sep)
  8. listToString(String itemName, List list)
  9. toString(final List list)