Java List to String listToString(List l)

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

Description

Take the List of objects and return a String of all the list's elements toString values appended to each other, separated by semicolons

License

Open Source License

Parameter

Parameter Description
l list of objects

Return

semicolon separated String of Strings.

Declaration

public static String listToString(List l) 

Method Source Code

//package com.java2s;

import java.util.*;

public class Main {
    /**/*from   w  ww .ja  va 2s.com*/
     * Take the List of objects and return a String of all the
     * list's elements toString values appended to each other,
     * separated by semicolons
     *
     * @param l list of objects
     * @return semicolon separated String of Strings.
     */
    public static String listToString(List l) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < l.size(); i++) {
            if (i > 0) {
                sb.append(";");
            }
            sb.append(l.get(i).toString());
        }

        return sb.toString();
    }

    /**
     * A utility method to an append to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null the we append to the StringBuffer the results of s1.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 object to append
     * @return StringBuffer with appended object
     */
    public static StringBuffer append(StringBuffer sb, Object s1) {
        if (sb == null) {
            sb = new StringBuffer();
        }
        sb.append((s1 == null) ? "null" : s1.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1, Object s2) {
        sb = append(sb, s1);
        sb.append((s2 == null) ? "null" : s2.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @param s3 third object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1, Object s2, Object s3) {
        sb = append(sb, s1, s2);
        sb.append((s3 == null) ? "null" : s3.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @param s3 third object to append
     * @param s4 fourth object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1, Object s2, Object s3, Object s4) {
        sb = append(sb, s1, s2, s3);
        sb.append((s4 == null) ? "null" : s4.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @param s3 third object to append
     * @param s4 fourth object to append
     * @param s5 fifth object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1, Object s2, Object s3, Object s4, Object s5) {
        sb = append(sb, s1, s2, s3, s4);
        sb.append((s5 == null) ? "null" : s5.toString());
        return sb;
    }

    /**
     * Convert the list of objects to a list of strings.
     *
     * @param l List of objects
     * @return List of strings.
     */
    public static List toString(List l) {
        List stringList = new ArrayList();
        for (int i = 0; i < l.size(); i++) {
            stringList.add(l.get(i).toString());
        }
        return stringList;
    }

    /**
     * Create a string representation of the given array
     *
     * @param array array to print
     * @return array as a String
     */
    public static String toString(Object[] array) {
        StringBuffer buf = new StringBuffer();
        buf.append(": ");
        for (int i = 0; i < array.length; i++) {
            buf.append("[");
            buf.append(i);
            buf.append("]: ");
            buf.append((array[i] == null) ? "null" : array[i]);
            buf.append(" ");
        }
        return buf.toString();
    }
}

Related

  1. listToString(final List stringList)
  2. listToString(Iterable list)
  3. listToString(List a, String separator)
  4. listToString(List inList, String delimeter)
  5. listToString(List l)
  6. listToString(List list)
  7. listToString(List list)
  8. listToString(List list)
  9. listToString(List list, char sep)