Java List Combine combineStringList(List sList)

Here you can find the source of combineStringList(List sList)

Description

combine String List

License

Open Source License

Declaration

public static String combineStringList(List<String> sList) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

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

public class Main {
    public final static String STRING_SPLIT_TOKEN = "--STR--";

    public static String combineStringList(List<String> sList) {
        return combineList(STRING_SPLIT_TOKEN, sList);
    }//w  ww.j  a  v a2 s. c om

    public static String combineList(String token, List<String> parts) {
        return "[" + combine(token, parts.toArray(new String[parts.size()])) + "]";
    }

    public static String combine(String token, String... parts) {
        int size = 0;
        for (String p : parts)
            size += p != null ? p.length() : 0;
        StringBuffer sb = new StringBuffer(size + (token.length() * parts.length) + 50);
        for (int i = 0; (i < parts.length); i++) {
            sb.append(parts[i]);
            if (i < parts.length - 1)
                sb.append(token);
        }
        return sb.toString();
    }

    /**
     * returns the length of the given string.
     * 0 if str is null
     * @param str
     * @return
     */
    public static int length(String str) {
        return str == null ? 0 : str.length();
    }

    public static String toString(Object[] objs, String separatedBy) {
        if (objs != null) {
            return toString(Arrays.asList(objs), separatedBy);
        } else {
            return "";
        }
    }

    public static String toString(int[] objs, String separatedBy) {
        ArrayList<Integer> l = new ArrayList<Integer>();
        for (int i : objs) {
            l.add(i);
        }
        return toString(l, separatedBy);
    }

    /**
     * Same as {@link #toString(java.util.Collection ,String) toString}
     * separated by ", ".
     *
     * @param c
     * @return
     */
    public static String toString(Collection c) {
        return toString(c, ", ");
    }

    /**
     * Returns a String representation of this collection.
     * It will use String.valueOf(Object) to convert object to string.
     * If separatedby is given, the items will be separated by that string.
     *
     * @param c
     * @param separatedBy
     * @return
     */
    public static String toString(Collection c, String separatedBy) {
        if (c == null || c.size() == 0)
            return "";

        StringBuffer sb = new StringBuffer();
        for (Iterator itr = c.iterator(); itr.hasNext();) {
            Object o = itr.next();
            sb.append(String.valueOf(o));
            if (separatedBy != null && itr.hasNext()) {
                sb.append(separatedBy);
            }
        }

        return sb.toString();
    }

    public static String toString(Object obj) {
        return obj == null ? "null" : obj.getClass().getName() + "@" + Integer.toHexString(obj.hashCode());
    }
}

Related

  1. combineLines(List lines)
  2. combineList(List l1, List l2)
  3. combineLists(List keys, List values)
  4. combineLists(List a, List b)
  5. combineString(List lstInput, String strToken)
  6. combineSubtractArrays(List a1, List a2)
  7. getCombinedColumnName(final List columnNames, final boolean sortByName)
  8. putCombinedList(Map map, Object key, Object value)
  9. recursiveCombine(List>> toCombinate, int index)