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:Main.java

public static <V> List<V> subList(List<V> list, int startIndex, int count) {
    if (null == list || startIndex >= list.size() || count <= 0) {
        return Collections.emptyList();
    }// w  ww  . java  2 s.  c om
    int endIndex = Math.min(startIndex + count, list.size());
    return list.subList(startIndex, endIndex);
}

From source file:Deal.java

public static ArrayList<Card> dealHand(List<Card> deck, int n) {
    int deckSize = deck.size();
    List<Card> handView = deck.subList(deckSize - n, deckSize);
    ArrayList<Card> hand = new ArrayList<Card>(handView);
    handView.clear();/*from   ww w. j  a v a  2  s. c o  m*/
    return hand;
}

From source file:com.bstek.dorado.data.model.TestDataHolder.java

public static void getDomainTestData3(Page page) throws IOException {
    List<Employee> employees = (List<Employee>) getDomainTestData3();
    page.setEntities(employees.subList(page.getFirstEntityIndex(), page.getLastEntityIndex()));
    page.setEntityCount(employees.size());
}

From source file:Main.java

public static List getSubList(List list, int fromIndex, int toIndex) {
    if (fromIndex > toIndex) {
        return null;
    }//  ww  w.  j a  va2 s . c o m

    int size = size(list);

    if (fromIndex < 0 || toIndex > size) {
        return null;
    }

    return list.subList(fromIndex, toIndex);
}

From source file:edu.cmu.cs.lti.ark.fn.evaluation.PrepareFullAnnotationJson.java

private static NamedSpanSet makeSpan(int start, int end, String name, List<String> tokens) {
    final ImmutableList<Span> spans = ImmutableList
            .of(new Span(start, end, Joiner.on(" ").join(tokens.subList(start, end))));
    return new NamedSpanSet(name, spans);
}

From source file:Main.java

public static <T> void rotate(Collection<T> collection, int rotateStep) {
    List<T> list = new LinkedList<T>();
    list.addAll(collection);//  w  w  w .ja v a2s . c o  m
    collection.clear();
    if (rotateStep > 0) {
        if (rotateStep > list.size())
            rotateStep %= list.size();
        collection.addAll(list.subList(list.size() - rotateStep, list.size()));
        collection.addAll(list.subList(0, list.size() - rotateStep));
    } else {
        if (Math.abs(rotateStep) > list.size())
            rotateStep %= list.size();
        rotateStep = Math.abs(rotateStep);
        collection.addAll(list.subList(rotateStep, list.size()));
        collection.addAll(list.subList(0, rotateStep));
    }
}

From source file:Main.java

public static <E> List<E> limitResults(List<E> list, int maxIndex) {
    if (maxIndex < 0) {
        throw new IllegalArgumentException("Expected non negative max index, received " + maxIndex); //$NON-NLS-1$
    }// ww w.ja  v  a2s .  c  o  m
    if (maxIndex > 0 && maxIndex <= list.size()) {
        return list.subList(0, maxIndex);
    }
    return list;
}

From source file:org.pad.pgsql.loadmovies.LoadFiles.java

/**
 * Load ratings and enrich movies with tags informations before updating the related movie.
 *
 * @throws Exception//from w  w w .  jav  a 2s.  c  o  m
 */
private static void loadRatings() throws Exception {
    //MultivalueMap with key movieId and values all tags
    LinkedMultiValueMap<Integer, Tag> tags = readTags();

    //MultivalueMap with key movieId and values all ratings
    LinkedMultiValueMap<Integer, Rating> ratings = new LinkedMultiValueMap();

    //"userId,movieId,rating,timestamp
    final Reader reader = new FileReader("C:\\PRIVE\\SRC\\ml-20m\\ratings.csv");
    CSVParser parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader());
    RatingDao ratingDao = new RatingDao(DS);
    for (CSVRecord record : parser) {
        Integer movieId = Integer.parseInt(record.get("movieId"));
        Integer userId = Integer.parseInt(record.get("userId"));
        if (keepId(movieId) && keepId(userId)) {
            //Building a rating object.
            Rating rating = new Rating();
            rating.setUserId(userId);
            rating.setMovieId(movieId);
            rating.setRating(Float.parseFloat(record.get("rating")));
            rating.setDate(new Date(Long.parseLong(record.get("timestamp")) * 1000));
            //Add for json saving
            ratings.add(rating.getMovieId(), rating);
            //traditional saving
            //ratingDao.save(rating);
        }
    }
    MovieDaoJSON movieDaoJSON = new MovieDaoJSON(DS);
    ratings.entrySet().stream().forEach((integerListEntry -> {
        //Building other information objects
        OtherInformations otherInformations = new OtherInformations();
        List ratingList = integerListEntry.getValue();
        otherInformations.setRatings(ratingList.subList(0, Math.min(10, ratingList.size())));
        otherInformations.computeMean();
        //Retrieve tags from the movieId
        otherInformations.setTags(tags.get(integerListEntry.getKey()));
        try {
            movieDaoJSON.addOtherInformationsToMovie(integerListEntry.getKey(), otherInformations);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }));

}

From source file:Main.java

/**
 * Introduces overlap into a series of lists. 
 * @param before # of elements from the end of the previous list to prepend
 * @param after # of elements from the beginning of the next list to append
 *//*from w w w .j ava 2  s  .  c om*/
public static <T> List<List<T>> overlap(List<List<T>> lists, int before, int after) {

    if (before < 0) {
        throw new IllegalArgumentException("Value of before cannot be negative");
    }
    if (after < 0) {
        throw new IllegalArgumentException("Value of after cannot be negative");
    }

    ListIterator<List<T>> iter = lists.listIterator();

    List<List<T>> result = new ArrayList<List<T>>();
    for (; iter.hasNext();) {
        List<T> current = new ArrayList<T>(iter.next());
        List<T> prev = before > 0 ? findPrevious(iter) : null;
        List<T> next = after > 0 ? findNext(iter) : null;
        if (prev != null) {
            List<T> overlap = prev.subList(prev.size() - before, prev.size());
            current.addAll(0, overlap);
        }
        if (next != null) {
            List<T> overlap = next.subList(0, after);
            current.addAll(overlap);
        }
        result.add(current);
    }

    return result;
}

From source file:Main.java

public static <O> List<O> safeSubList(List<O> l, int offset, int count) {
    if (l == null) {
        return null;
    }/*w w  w.ja  v a 2s . com*/

    if (offset >= l.size())
        return Collections.emptyList();

    if (count == 0)
        count = l.size();

    return l.subList(offset, Math.min(offset + count, l.size()));
}