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:de.tudarmstadt.ukp.argumentation.data.roomfordebate.DataFetcher.java

/**
 * Saves first N comments of a given article to a text file
 *
 * @param comments   comments/*from w  ww  .j  av  a 2  s.  c  o  m*/
 * @param outputFile output file
 * @param article    corresponding article
 * @throws IOException exception
 */
private static void saveCommentsToText(List<Comment> comments, File outputFile, Article article)
        throws IOException {
    PrintWriter pw = new PrintWriter(outputFile, "utf-8");

    // take first 10 comments
    List<Comment> firstTen = comments.subList(0,
            comments.size() > FIRST_N_COMMENTS ? FIRST_N_COMMENTS : comments.size());

    // collect IDs for mapping to 1-10
    List<String> ids = new ArrayList<>();
    for (Comment comment : firstTen) {
        ids.add(comment.getId());
    }

    // header
    pw.printf("Debate title: %s%n%nDebate description: %s%n%nArticle title: %s%n%n", article.getDebateTitle(),
            article.getDebateDescription(), article.getTitle());

    for (Comment comment : firstTen) {
        pw.printf("#%s %s%s%n%n%s%n%n%n", ids.indexOf(comment.getId()) + 1,
                comment.getCommenterName().replaceAll("\\s+", "_"),
                comment.getParentId() != null ? " ReactsTo #" + (ids.indexOf(comment.getParentId()) + 1) : "",
                StringUtils.join(comment.getText().split("\n"), "\n\n"));
    }

    IOUtils.closeQuietly(pw);
}

From source file:Main.java

/**
 * Returns a {@link List} containing the elements of the given {@link List} from offset and only the given amount.
 * If there aren't enough elements in the given {@link List} for the requested amount the rest of the {@link List}
 * will returned. Returns null if the given {@link List} is null.
 *
 * @param list the {@link List} to get a sublist from.
 * @param offset start of elements to return
 * @param amount amount of elements to return
 * @return the sublist or null.//from www .  ja  va  2 s.  c om
 */
public static <T> List<T> subList(final List<T> list, final int offset, final int amount) {
    if (list == null) {
        return null;
    }

    if (offset >= list.size()) {
        return Collections.emptyList();
    }

    int toPos = offset + amount;
    if (toPos >= list.size()) {
        toPos = list.size();
    }

    return list.subList(offset, toPos);
}

From source file:ch.jamiete.hilda.music.commands.MusicQueueCommand.java

private static <T> List<T> getPage(final List<T> sourceList, final int page, final int pageSize) {
    final int fromIndex = page * pageSize;

    if ((sourceList == null) || (sourceList.size() < fromIndex)) {
        return Collections.emptyList();
    }/* w ww.j  a  va2s  . c om*/

    return sourceList.subList(fromIndex, Math.min(fromIndex + pageSize, sourceList.size()));
}

From source file:ch.systemsx.cisd.openbis.generic.client.web.server.resultset.CachedResultSetManager.java

/**
 * Encapsulates list returned by {@link List#subList(int, int)} in a new <code>List</code> as
 * <i>GWT</i> complains because of a serialization concern.
 *//*from  w w w  . j a  v  a  2  s  .c  o m*/
private final static <T> List<T> subList(final List<T> data, final int offset, final int limit) {
    final int toIndex = offset + limit;
    return new ArrayList<T>(data.subList(offset, toIndex));
}

From source file:com.dianping.dpsf.other.echo.SimpleEchoClient.java

private static List<String[]> generateHostList() {
    List<String[]> hostList = new ArrayList<String[]>();
    hostList.add(new String[] { "127.0.0.1", "20001" });
    hostList.add(new String[] { "127.0.0.1", "20002" });
    hostList.add(new String[] { "127.0.0.1", "20003" });

    Random rnd = new Random(System.currentTimeMillis());
    int num = rnd.nextInt(3) + 1;
    Collections.shuffle(hostList);
    return hostList.subList(0, num);
}

From source file:Main.java

/**
 * Split the list and return the stream with the resultant lists.
 *
 * @param list to be split/*  w  w w  . j ava  2 s. c  o  m*/
 * @param size of each list after splitting.
 * @param <T>  type of list.
 * @return {@link Stream} of {@link List}s
 */
public static <T> Stream<List<T>> splitListStream(final List<T> list, final int size) {

    if (size <= 0) {
        throw new IllegalArgumentException("Invalid Split Size");
    }

    final int listSize = list.size();

    if (listSize == 0) {
        return Stream.empty();
    }

    return IntStream.rangeClosed(0, (listSize - 1) / size)
            .mapToObj(n -> list.subList(n * size, Math.min((n + 1) * size, listSize)));
}

From source file:Main.java

public static <T> List<List<T>> partition(List<T> inputList, int size) {
    if (inputList.size() == 0) {
        return Collections.singletonList(inputList);
    }/* www.  j ava 2 s  .  com*/

    int partitions = inputList.size() / size + (inputList.size() % size > 0 ? 1 : 0);

    List<List<T>> result = new ArrayList<List<T>>(partitions);

    for (int i = 0; i < partitions - 1; i++) {
        result.add(inputList.subList(i * size, (i + 1) * size));
    }

    result.add(inputList.subList((partitions - 1) * size, inputList.size()));

    return result;
}

From source file:Main.java

public static <T> List<List<T>> partition(List<T> items, int slices) {
    double size = items.size() / (double) slices;
    double accum = 0;
    int start = 0;
    List<List<T>> list = new ArrayList<>(slices);
    for (int i = 0; i < slices; i++) {
        int end = (int) Math.floor(accum + size);
        if (i == slices - 1)
            end = items.size();/*from w w w  .  j  a  v a  2s  .  co m*/
        list.add(items.subList(start, end));
        accum += size;
        start = (int) Math.floor(accum);
    }
    return list;
}

From source file:com.waku.mmdataextract.CompareProductions.java

public static void start(List<String> prodIdList, int start) {
    for (int step = start; step < prodIdList.size(); step = step + 100) {
        List<String> temp = prodIdList.subList(step,
                (step + 100) > prodIdList.size() ? prodIdList.size() : step + 100);
        File file = new File("output/CompareProductions" + (step + temp.size()) + ".csv");
        // if (file.exists()) {
        // logger.info("Data already finished in " + file.getName());
        // continue;
        // }/*from w ww .ja  v a  2  s .c o  m*/
        logger.info("Started with -> " + temp);

        for (String[] ids : combineProdIds(temp)) {
            processCompare(false, ids);
        }

        String[] headLine = HEAD_LIST.toArray(new String[0]);
        List<String[]> prodList = new ArrayList<String[]>();

        for (Map<String, String> prodMap : RESULT_LIST) {
            String[] prod = new String[headLine.length];
            for (Map.Entry<String, String> entry : prodMap.entrySet()) {
                prod[HEAD_LIST.indexOf(entry.getKey())] = entry.getValue();
            }
            for (int i = 0; i < prod.length; i++) {
                if (prod[i] == null) {
                    prod[i] = "N/A";
                }
            }
            prodList.add(prod);
        }

        CsvListWriter writer = null;
        try {
            writer = new CsvListWriter(new FileWriter(file), CsvPreference.STANDARD_PREFERENCE);
            writer.writeHeader(headLine);
            for (String[] s : prodList) {
                writer.write(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                // ignore
            }
        }
        logger.info("---> Done for " + (step + temp.size()));
    }
    logger.info("------------------> Haha, done!");
}

From source file:com.addthis.hydra.data.util.FindChangePoints.java

private static List<ChangePoint> findChangePoints(Long[] data, int minChange, double minRatio, double minZScore,
        int inactiveThreshold, int windowSize) {
    List<Long> dataList = Arrays.asList(data);
    ArrayList<ChangePoint> rvList = new ArrayList<>();
    for (int i = 2; i < data.length; i++) {
        int startIndex = Math.max(i - windowSize + 1, 0);
        Long[] currSlice = dataList.subList(startIndex, i).toArray(new Long[] {});
        long nextValue = data[i];
        double predicted = linearPredictNext(currSlice);
        double diff = nextValue - predicted;
        double zScoreDiff = diff / sd(currSlice);
        double changeRatio = -1 + (double) (nextValue) / Math.max(predicted, 1.);
        if (Math.abs(zScoreDiff) > minZScore && Math.abs(diff) > minChange
                && Math.abs(changeRatio) > minRatio) {
            ChangePoint.ChangePointType type = chooseTypeForChange((long) mean(currSlice), nextValue,
                    inactiveThreshold);/* w ww.j a  v  a  2  s.  co  m*/
            rvList.add(new ChangePoint((int) diff, i, type));
        }
    }
    return rvList;
}