Java List Implode implode(String split, List list)

Here you can find the source of implode(String split, List list)

Description

implode

License

LGPL

Declaration

public static String implode(String split, List<?> list) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

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

public class Main {

    public static String implode(String split, List<?> list) {
        return implode(split, list, "", "");
    }//from   ww w. jav a  2 s. com

    public static String implode(String split, List<?> list, String prefix,
            String subffix) {
        StringBuilder sb = new StringBuilder();
        if (null == list)
            return "";
        int size = list.size();
        if (1 > size)
            return "";
        for (int i = 0; i < size; i++) {
            Object value = list.get(i);
            if (null == value)
                continue;
            if (value instanceof List) {
                sb.append(implode(split, (List<?>) value));
            } else if (value instanceof Map) {
                sb.append(implode(split, (Map<?, ?>) value));
            } else {
                sb.append(prefix).append(value).append(subffix);
            }
            if (i + 1 < size)
                sb.append(split);
        }
        return sb.toString();
    }

    public static String implode(String split, Set<?> set) {
        return implode(split, set, "", "");
    }

    public static String implode(String split, Set<?> set, String prefix,
            String subffix) {
        StringBuilder sb = new StringBuilder();
        if (null == set)
            return "";
        if (1 > set.size())
            return "";
        Iterator<?> item = set.iterator();
        boolean hasNext = item.hasNext();
        while (hasNext) {
            Object value = item.next();
            if (null == value)
                continue;
            if (value instanceof List) {
                sb.append(implode(split, (List<?>) value));
            } else if (value instanceof Map) {
                sb.append(implode(split, (Map<?, ?>) value));
            } else {
                sb.append(prefix).append(value).append(subffix);
            }
            hasNext = item.hasNext();
            if (hasNext)
                sb.append(split);
        }
        return sb.toString();
    }

    public static String implode(String split, Map<?, ?> map) {
        return implode(split, map, "", "");
    }

    public static String implode(String split, Map<?, ?> map,
            String prefix, String subffix) {
        StringBuilder sb = new StringBuilder();
        Set<?> set = map.keySet();
        int i = 1, size = set.size();
        for (Object key : set) {
            if (null == key)
                continue;
            Object value = map.get(key);
            if (null == value)
                continue;
            if (value instanceof List) {
                sb.append(implode(split, (List<?>) value));
            } else if (value instanceof Map) {
                sb.append(implode(split, (Map<?, ?>) value));
            } else {
                sb.append(prefix).append(value).append(subffix);
            }
            if (i++ < size)
                sb.append(split);
        }
        return sb.toString();
    }
}

Related

  1. implode(List list, String glue)
  2. implode(List list, String glue)
  3. implode(List strings, String separator)
  4. implode(List tokens, char separator, char escapeCharacter)
  5. implode(String glue, List list)
  6. implodeList(final List list, final String glue)