Java Collection Combine combine(String separator, Collection objs)

Here you can find the source of combine(String separator, Collection objs)

Description

Combine the objects in Collection with the specified separator.

License

Apache License

Parameter

Parameter Description
separator a parameter
objs the collection to be combined

Declaration

public static <T> String combine(String separator, Collection<T> objs) 

Method Source Code


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

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

public class Main {
    /**/* ww  w.  j a va  2  s.c om*/
     * Combine objects with the specified separator.
     * 
     * @param separator
     * @param objs
     *            the objects to be combined
     * @return
     */
    // @SafeVarargs
    public static <T> String combine(String separator, T... objs) {
        return combine(separator, Arrays.asList(objs));
    }

    /**
     * Combine the objects in {@code Collection} with the specified separator.
     * 
     * @param separator
     * @param objs
     *            the collection to be combined
     * @return
     */
    public static <T> String combine(String separator, Collection<T> objs) {
        StringBuffer sb = new StringBuffer();
        if (objs != null && objs.size() > 0) {
            Iterator<T> it = objs.iterator();
            for (;;) {
                sb.append(it.next().toString());
                if (it.hasNext())
                    sb.append(separator);
                else
                    break;
            }
        }
        return sb.toString();
    }
}

Related

  1. combine(Collection... collections)
  2. combine(Collection strings)
  3. combine(final Collection[] c)
  4. combine(String separator, Collection parts)
  5. combine(String separator, Collection stringCollection)
  6. combineAndRemoveDuplicates(Collection collection1, Collection collection2)
  7. combineCollections(Collection sessionCollection, Collection reqCollection)
  8. CombineCollections(final Collection... collections)
  9. combineNames(Collection items)

    HOME | Copyright © www.java2s.com 2016