Java List to String toString(final Object[] list)

Here you can find the source of toString(final Object[] list)

Description

Returns a readable presentation of an Object array.

License

Open Source License

Parameter

Parameter Description
list List of Objects.

Return

List notation.

Declaration

public static String toString(final Object[] list) 

Method Source Code

//package com.java2s;
/* This file is part of the project "Hilbert II" - http://www.qedeq.org
 *
 * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
 *
 * "Hilbert II" 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 2 of the License, or (at your option) any later version.
 *
 * This program 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.
 *//*  ww w  . j av  a2s. co m*/

import java.util.Iterator;

import java.util.Map;

import java.util.Set;

public class Main {
    /**
     * Returns a readable presentation of an Object array. Something like ("a", null, 13)" if we have
     * the "a", null, 13. Objects of type {@link CharSequence} are quoted.
     *
     * @param   list  List of Objects.
     * @return  List notation.
     */
    public static String toString(final Object[] list) {
        final StringBuffer buffer = new StringBuffer(30);
        buffer.append("(");
        if (list != null) {
            for (int i = 0; i < list.length; i++) {
                if (i > 0) {
                    buffer.append(", ");
                }
                if (list[i] instanceof CharSequence) {
                    buffer.append("\"");
                    buffer.append(list[i].toString());
                    buffer.append("\"");
                } else {
                    buffer.append(String.valueOf(list[i]));
                }
            }
        }
        buffer.append(")");
        return buffer.toString();
    }

    /**
     * Returns a readable presentation of a Set. Something like {"a", null, "13} if we have
     * "a", null, 13. Objects of type {@link CharSequence} are quoted.
     *
     * @param   set Set of objects.
     * @return  Set notation for toString() results.
     */
    public static String toString(final Set set) {
        final StringBuffer buffer = new StringBuffer(30);
        buffer.append("{");
        if (set != null) {
            Iterator e = set.iterator();
            boolean notFirst = false;
            while (e.hasNext()) {
                if (notFirst) {
                    buffer.append(", ");
                } else {
                    notFirst = true;
                }
                final Object obj = e.next();
                if (obj instanceof CharSequence) {
                    buffer.append("\"");
                    buffer.append(String.valueOf(obj));
                    buffer.append("\"");
                } else {
                    buffer.append(String.valueOf(obj));
                }
            }
        }
        buffer.append("}");
        return buffer.toString();
    }

    /**
     * Returns a readable presentation of a Map. Something like "{a=2, b=null, c=12}" if the
     * Map contains (a, 2), (b, null), (c, 12).
     *
     * @param   map Map of objects mappings.
     * @return  Set notation for toString() results.
     */
    public static String toString(final Map map) {
        final StringBuffer buffer = new StringBuffer(30);
        buffer.append("{");
        if (map != null) {
            Iterator e = map.entrySet().iterator();
            boolean notFirst = false;
            while (e.hasNext()) {
                if (notFirst) {
                    buffer.append(", ");
                } else {
                    notFirst = true;
                }
                final Map.Entry entry = (Map.Entry) e.next();
                buffer.append(String.valueOf(entry.getKey()));
                buffer.append("=");
                final Object value = entry.getValue();
                if (value instanceof CharSequence) {
                    buffer.append("\"");
                    buffer.append(String.valueOf(value));
                    buffer.append("\"");
                } else {
                    buffer.append(String.valueOf(value));
                }
            }
        }
        buffer.append("}");
        return buffer.toString();
    }
}

Related

  1. toString(final List list, final String separtor)
  2. toString(final List list)
  3. toString(final List inMessages)
  4. toString(final List input)
  5. toString(final List list, char delimiter)
  6. toString(List arguments)
  7. toString(List l)
  8. toString(List list)
  9. toString(List list)