Java List from Array asList(List list)

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

Description

as List

License

Open Source License

Parameter

Parameter Description
list The list of elements

Return

The list of elements formatted as a comma-delimited list

Declaration

@SuppressWarnings("nls")
public static String asList(List<String> list) 

Method Source Code


//package com.java2s;
import java.util.Arrays;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class Main {
    /**//ww  w. j av  a  2  s.  c  o  m
     * @param list
     *            The list of elements
     * @return The list of elements formatted as a comma-delimited list
     */
    @SuppressWarnings("nls")
    public static String asList(List<String> list) {
        StringBuffer buffer = new StringBuffer();
        if (list != null) {
            if (list.size() == 1) {
                buffer.append(list.get(0));
            } else {
                for (String element : list) {
                    buffer.append(element);
                    buffer.append(", ");
                }

                if (buffer.length() > 2) {
                    buffer.delete(buffer.length() - 2, buffer.length());
                }
            }
        }
        return buffer.toString();
    }

    /**
     * @param map
     *            A map of strings
     * @return A map expressed as a comma-delimited list of name=value tuples
     */
    @SuppressWarnings("nls")
    public static String asList(Map<String, String> map) {
        StringBuffer buffer = new StringBuffer();
        if (map != null) {
            Set<String> keys = map.keySet();
            for (String key : keys) {
                buffer.append(String.format("%s=%s, ", key, map.get(key)));
            }

            if (buffer.length() > 2) {
                buffer.delete(buffer.length() - 2, buffer.length());
            }
        }
        return buffer.toString();
    }

    /**
     * This method takes a variable argument list of string values and returns a single string with the values expressed
     * as a comma-delimited list.
     * 
     * @param values
     *            The values to be converted into a comma-separated list
     * @return The comma-separated list, or an empty string if there are no values
     */
    public static String asList(String... values) {
        return asList(Arrays.asList(values));
    }
}

Related

  1. asList(Iterable iterable)
  2. asList(Iterable iterable)
  3. asList(Iterable iterable)
  4. asList(Iterable iterable)
  5. asList(Iterator iterator)
  6. asList(long... array)
  7. asList(Object info)
  8. asList(Object o, Class cls)
  9. asList(Object theone)