Java Collection Concatenate concatenateIds(final Collection ids)

Here you can find the source of concatenateIds(final Collection ids)

Description

concatenate Ids

License

Apache License

Declaration

public static String concatenateIds(final Collection ids) 

Method Source Code


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

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Main {
    public static String concatenateIds(final Collection ids) {
        String concatIds = "";

        for (Object id : ids) {
            concatIds += "'" + id.toString() + "',";
        }// w  w  w  . j a v a 2 s .  c  o  m

        return concatIds.substring(0, concatIds.length() - 1);
    }

    public static List<String> concatenateIds(final List ids, int limit) {
        List<String> queries = new ArrayList<String>();

        List<List> chunks = chunks(ids, limit);
        for (List chunk : chunks) {
            String query = "";
            for (Object id : chunk) {
                query += "'" + id.toString() + "',";
            }
            queries.add(query.substring(0, query.length() - 1));
        }

        return queries;
    }

    /**
     * Split a big list into fix sized chunks
     *
     * @param bigList   big list to split
     * @param chunkSize chunk size
     * @param <T>       element type
     * @return a list of lists
     */
    public static <T> List<List<T>> chunks(List<T> bigList, int chunkSize) {
        List<List<T>> chunks = new ArrayList<List<T>>();

        for (int i = 0; i < bigList.size(); i += chunkSize) {
            List<T> chunk = bigList.subList(i, Math.min(bigList.size(), i + chunkSize));
            chunks.add(chunk);
        }

        return chunks;
    }
}

Related

  1. concatenate(Collection collections)
  2. concatenate(Collection coll)
  3. concatenate(Collection list)
  4. concatenate(final Collection objects, final String separator)
  5. concatenatedLTL(Collection ltl)
  6. concatenateWithComma(Collection collection)
  7. concatEntries(Collection coll, String sep, String lastSep)
  8. concatInt(Collection collection)
  9. concatNames(Collection cNames, String split)