Java List to String toString(List list)

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

Description

to String

License

Open Source License

Parameter

Parameter Description
list the list

Return

the list as comma separated string

Declaration

public static String toString(List<?> list) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2006-2012/*from   w  ww . ja v a2s.c o  m*/
 * Software Technology Group, Dresden University of Technology
 * DevBoost GmbH, Berlin, Amtsgericht Charlottenburg, HRB 140026
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *   Software Technology Group - TU Dresden, Germany;
 *   DevBoost GmbH - Berlin, Germany
 *      - initial API and implementation
 ******************************************************************************/

import java.util.List;

public class Main {
    /**
     * @param list the list
     * @return the list as comma separated string
     */
    public static String toString(List<?> list) {
        if (list == null) {
            return "null";
        }
        String listStr = "";
        for (Object obj : list) {
            listStr += obj.toString() + ", ";
        }
        listStr = trimLastSeperator(listStr, ", ");
        return listStr;
    }

    /**
     * @param arg the string
     * @param sep the separator used in the string
     * @return the string with the separator trimmed from the end
     */
    public static String trimLastSeperator(String arg, String sep) {
        if (arg == null || sep == null || arg.equals("") || sep.equals("")) {
            return "";
        }
        return arg.substring(0, arg.lastIndexOf(sep));
    }
}

Related

  1. toString(List list)
  2. toString(List list)
  3. toString(List objects)
  4. toString(List col)
  5. toString(List coll, char delimiter)
  6. toString(List list)
  7. toString(List list)
  8. toString(List list)
  9. toString(List list, char sep)