Java Collection to String toStrings(Collection objects)

Here you can find the source of toStrings(Collection objects)

Description

Converts the given objects into a string list by invoking Object#toString() on each non-null element.

License

Apache License

Declaration

public static <T> List<String> toStrings(Collection<T> objects) 

Method Source Code

//package com.java2s;
/*-------------------------------------------------------------------------+
|                                                                          |
| Copyright 2005-2011 The ConQAT Project                                   |
|                                                                          |
| Licensed under the Apache License, Version 2.0 (the "License");          |
| you may not use this file except in compliance with the License.         |
| You may obtain a copy of the License at                                  |
|                                                                          |
|    http://www.apache.org/licenses/LICENSE-2.0                            |
|                                                                          |
| Unless required by applicable law or agreed to in writing, software      |
| distributed under the License is distributed on an "AS IS" BASIS,        |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and      |
| limitations under the License.                                           |
+-------------------------------------------------------------------------*/

import java.util.ArrayList;

import java.util.Collection;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Main {
    /** Line break. */
    public static final String CR = System.getProperty("line.separator");
    /** The empty string. */
    public static final String EMPTY_STRING = "";

    /**/*from   w  w  w.  ja v  a  2  s  .com*/
     * Converts the given objects into a string list by invoking
     * {@link Object#toString()} on each non-null element. For null entries in
     * the input, the output will contain a null entry as well.
     */
    public static <T> List<String> toStrings(Collection<T> objects) {
        List<String> strings = new ArrayList<>();
        for (T t : objects) {
            if (t == null) {
                strings.add(null);
            } else {
                strings.add(t.toString());
            }
        }
        return strings;
    }

    /**
     * Create string representation of a map.
     */
    public static String toString(Map<?, ?> map) {
        return toString(map, EMPTY_STRING);
    }

    /**
     * Create string representation of a map.
     * 
     * @param map
     *            the map
     * @param indent
     *            a line indent
     */
    public static String toString(Map<?, ?> map, String indent) {
        StringBuilder result = new StringBuilder();
        Iterator<?> keyIterator = map.keySet().iterator();

        while (keyIterator.hasNext()) {
            result.append(indent);
            Object key = keyIterator.next();
            result.append(key);
            result.append(" = ");
            result.append(map.get(key));
            if (keyIterator.hasNext()) {
                result.append(CR);
            }
        }

        return result.toString();
    }
}

Related

  1. toString(final Collection list, final String delimiter)
  2. toString(final Collection c)
  3. toString(String[] collection, char separator)
  4. toStrings(Collection vals)
  5. toStrings(Collection c)
  6. toStrings(final Collection stringCollection)
  7. toStringWithDelimiters(Collection objects, String delim)
  8. toStringWithSeparator(Collection collection, String separator)