Example usage for java.util List subList

List of usage examples for java.util List subList

Introduction

In this page you can find the example usage for java.util List subList.

Prototype

List<E> subList(int fromIndex, int toIndex);

Source Link

Document

Returns a view of the portion of this list between the specified fromIndex , inclusive, and toIndex , exclusive.

Usage

From source file:com.puppetlabs.geppetto.forge.util.TarUtils.java

private static List<List<String>> splitList(List<String> files, int limit) {
    List<List<String>> result = new ArrayList<List<String>>();
    int top = files.size();
    int start = 0;
    while (start < top) {
        int max = Math.min(limit, top - start);
        result.add(files.subList(start, start + max));
        start += max;//  www.ja  v a 2 s  . c o  m
    }
    return result;
}

From source file:org.apache.hive.ptest.execution.JIRAService.java

static List<String> trimMessages(List<String> messages) {
    int size = messages.size();
    if (size > MAX_MESSAGES) {
        messages = messages.subList(size - MAX_MESSAGES, size);
        messages.add(0, TRIMMED_MESSAGE);
    }/*  w  w  w  .  j av  a2s.c o m*/
    return messages;
}

From source file:com.fengduo.bee.search.utils.PinyinParser.java

public static List<String> getPermutationSentence(List<String> termArrays, int start) {
    if (Argument.isEmpty(termArrays)) {
        return Collections.<String>emptyList();
    }/*from  www.jav  a2  s  .  c  o  m*/
    int size = termArrays.size();
    if (start < 0 || start >= size) {
        return Collections.<String>emptyList();
    }
    if (start == size - 1) {
        return termArrays.subList(0, start);
    }
    List<String> strings = termArrays.subList(0, start);
    List<String> permutationSentences = getPermutationSentence(termArrays, start + 1);
    if (Argument.isEmpty(strings)) {
        return permutationSentences;
    }
    if (Argument.isEmpty(permutationSentences)) {
        return strings;
    }
    List<String> result = new ArrayList<String>();
    for (String pre : strings) {
        for (String suffix : permutationSentences) {
            result.add(pre + suffix);
        }
    }
    return result;
}

From source file:com.healthmarketscience.jackcess.query.QueryTest.java

private static void removeLastRows(Query query, int num) {
    List<Row> rows = ((QueryImpl) query).getRows();
    int size = rows.size();
    rows.subList(size - num, size).clear();
}

From source file:iddb.core.util.Functions.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static List sublist(List list, int size) {
    List result = new ArrayList();
    for (int i : Range.range(0, list.size(), size)) {
        if (i + size > list.size()) {
            result.add(list.subList(i, i + (list.size() - i)));
        } else {/*from  ww w.j a va2  s  .c o  m*/
            result.add(list.subList(i, i + size));
        }
    }
    return result;
}

From source file:Main.java

public static final <T> List<List<T>> devide(List<T> origin, int size) {
    if (origin == null || origin.isEmpty() || size <= 0) {
        return Collections.emptyList();
    }/*ww  w.j a va2s  .  c o m*/

    final int block = origin.size() / size + (origin.size() % size > 0 ? 1 : 0);

    List<List<T>> devidedList = new ArrayList<List<T>>(block);
    for (int i = 0; i < block; i++) {
        final int start = i * size;
        final int end = Math.min(start + size, origin.size());
        devidedList.add(new ArrayList<T>(origin.subList(start, end)));
    }
    return devidedList;
}

From source file:Main.java

/**
 * Partition a list into <code>num</code> sub-lists.  If the list does
 * not divide evenly, the extra 'n' elements are split across the
 * first 'n' lists.  There will be no more lists than elements returned (i.e. no empty lists tacked on to the end)
 *//* w  w w . j  a  v a 2  s . co m*/
public static <T> List<List<T>> partition(List<T> list, int num) {
    if (num < 1) {
        throw new IllegalArgumentException("Number of sub-lists must be greater than zero");
    }
    List<List<T>> result = new ArrayList<List<T>>();
    int index = 0;
    int listsRemaining = num;
    int elementsRemaining = list.size();
    while (elementsRemaining > 0) {
        int size = (int) Math.ceil(elementsRemaining / (listsRemaining + 0.0));
        List<T> subList = list.subList(index, index + size);
        result.add(subList);
        listsRemaining--;
        elementsRemaining -= size;
        index += size;
    }
    if (elementsRemaining != 0) {
        throw new IllegalStateException(
                String.format("Loop exited with %d elements still remaining", elementsRemaining));
    }
    return result;
}

From source file:edu.northwestern.bioinformatics.studycalendar.security.plugin.AuthenticationSystemValidator.java

public static void validateRequiredElementsCreated(AuthenticationSystem system)
        throws AuthenticationSystemInitializationFailure {
    List<String> missing = new ArrayList<String>(3);
    if (system.authenticationManager() == null) {
        missing.add("authenticationManager()");
    }//  w ww  .j a  v  a 2s.c  om
    if (system.entryPoint() == null) {
        missing.add("entryPoint()");
    }
    if (!missing.isEmpty()) {
        String list = missing.get(missing.size() - 1);
        if (missing.size() >= 2) {
            list = String.format("%s or %s",
                    StringUtils.join(missing.subList(0, missing.size() - 1).iterator(), ", "), list);
        }
        throw new AuthenticationSystemInitializationFailure("%s must not return null from %s",
                system.getClass().getSimpleName(), list);
    }
}

From source file:kina.rdd.CassandraRDDUtils.java

public static <W> void doCql3SaveToCassandra(RDD<W> rdd, CassandraKinaConfig<W> writeConfig,
        Function1<W, Tuple2<Cells, Cells>> transformer) {
    if (!writeConfig.getIsWriteConfig()) {
        throw new IllegalArgumentException("Provided configuration object is not suitable for writing");
    }/*from w  ww. j  a va 2 s  .  c o  m*/
    Tuple2<Map<String, ByteBuffer>, Map<String, ByteBuffer>> tuple = new Tuple2<>(null, null);

    RDD<Tuple2<Cells, Cells>> mappedRDD = rdd.map(transformer,
            ClassTag$.MODULE$.<Tuple2<Cells, Cells>>apply(tuple.getClass()));

    ((GenericCassandraKinaConfig) writeConfig).createOutputTableIfNeeded(mappedRDD);

    final int pageSize = writeConfig.getBatchSize();
    int offset = 0;

    List<Tuple2<Cells, Cells>> elements = Arrays.asList((Tuple2<Cells, Cells>[]) mappedRDD.collect());
    List<Tuple2<Cells, Cells>> split;
    do {
        split = elements.subList(pageSize * (offset++), Math.min(pageSize * offset, elements.size()));

        Batch batch = QueryBuilder.batch();

        for (Tuple2<Cells, Cells> t : split) {
            Tuple2<String[], Object[]> bindVars = Utils.prepareTuple4CqlDriver(t);

            Insert insert = QueryBuilder
                    .insertInto(quote(writeConfig.getKeyspace()), quote(writeConfig.getTable()))
                    .values(bindVars._1(), bindVars._2());

            batch.add(insert);
        }
        writeConfig.getSession().execute(batch);

    } while (!split.isEmpty() && split.size() == pageSize);
}

From source file:com.cloud.utils.StringUtils.java

private static <T> List<List<T>> partitionList(final List<T> originalList, final int chunkSize) {
    final List<List<T>> listOfChunks = new ArrayList<List<T>>();
    for (int i = 0; i < originalList.size() / chunkSize; i++) {
        listOfChunks.add(originalList.subList(i * chunkSize, i * chunkSize + chunkSize));
    }//w ww . j a  va2  s.c om
    if (originalList.size() % chunkSize != 0) {
        listOfChunks.add(originalList.subList(originalList.size() - originalList.size() % chunkSize,
                originalList.size()));
    }
    return listOfChunks;
}