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:net.longfalcon.newsj.util.ArrayUtil.java

public static <T> List<T> paginate(List<T> list, int offset, int pageSize) {
    int arraySize = list.size();
    if (offset > arraySize) {
        return list;
    }//from  w ww .  ja  va  2 s.c om

    int endIndex = offset + pageSize;
    if (endIndex >= arraySize) {
        return list.subList(offset, arraySize);
    } else {
        return list.subList(offset, endIndex);
    }
}

From source file:Main.java

public static <T> java.util.List<T> randomSample(java.util.List<T> items, int size) {
    size = Math.min(size, items.size());
    java.util.Random rnd = new java.util.Random();
    for (int i = 0; i < size; i++) {
        int pos = i + rnd.nextInt(items.size() - i);
        T tmp = items.get(pos);//  www.j  a va  2s  .c o m
        items.set(pos, items.get(i));
        items.set(i, tmp);
    }
    return items.subList(0, size);
}

From source file:Main.java

public static <T> List<T> subList(List<T> list, int first, int end) {
    if (null == list || list.isEmpty())
        return null;
    int size = list.size();
    int fromIndex = first;
    int toIndex = end;
    if (fromIndex < 0) {
        toIndex = 0;//from  ww w. j  a  va 2 s . c  o m
    }
    if (toIndex > size) {
        toIndex = size;
    }
    if (fromIndex > toIndex)
        return null;
    return list.subList(fromIndex, toIndex);
}

From source file:com.netflix.spinnaker.clouddriver.kubernetes.v2.caching.KubernetesV2SearchProvider.java

private static <T> List<T> paginateResults(List<T> matches, Integer pageSize, Integer pageNumber) {
    Integer startingIndex = pageSize * (pageNumber - 1);
    Integer endIndex = Math.min(pageSize * pageNumber, matches.size());
    return startingIndex < endIndex ? matches.subList(startingIndex, endIndex) : new ArrayList<>();
}

From source file:Main.java

public static <E> List<E> compact(List<E> list) {
    boolean foundAtLeastOneNull = false;
    List<E> compacted = null;
    int i = 0;/*from ww  w . j  a  va  2 s  .  c  o m*/

    for (E element : list) {
        if (element == null) {
            if (!foundAtLeastOneNull) {
                compacted = new ArrayList<E>(list.size());
                if (i > 0) {
                    compacted.addAll(list.subList(0, i));
                }
            }
            foundAtLeastOneNull = true;
        } else if (foundAtLeastOneNull) {
            compacted.add(element);
        }
        ++i;
    }

    return foundAtLeastOneNull ? compacted : list;
}

From source file:com.google.android.apps.santatracker.games.cityquiz.CityQuizUtil.java

/**
 * Retrieve a random list of cities.// w ww .j  a va2 s. c  o m
 *
 * @param amt Max number of cities to retrieve.
 *
 * @return Random list of cities. If amt is more than the amount of cities available, all cities are returned.
 */
public static List<City> getCities(Context context, int amt) {
    List<City> allCities = getCities(context);
    Collections.shuffle(allCities, new Random());
    if (amt > allCities.size()) {
        amt = allCities.size();
    }
    // Only return the cities that will be used in the game.
    List<City> cities = new ArrayList<>();
    cities.addAll(0, allCities.subList(0, amt));
    return cities;
}

From source file:Main.java

/**
 * <p>//from  w  w w . ja  v  a 2  s.c o m
 * Get sub List form source list
 * </p>
 * 
 * @param sourceList source list
 * @param fromIndex start index, if less than 0, start form 0
 * @param toIndex end index, if large than sourceList.size(), ends with
 *            sourceList.size()
 * @return
 */
public static <E extends Object> List<E> subList(List<E> sourceList, int fromIndex, int toIndex) {
    if (fromIndex > toIndex) {
        fromIndex = toIndex;
    }
    int endIndex = sourceList.size();
    if (fromIndex > endIndex) {
        fromIndex = endIndex;
    } else if (fromIndex < 0) {
        fromIndex = 0;
    }
    if (toIndex > endIndex) {
        toIndex = endIndex;
    }
    return sourceList.subList(fromIndex, toIndex);
}

From source file:eu.delving.stats.ChartHelper.java

private static List<Stats.Counter> sort(Collection<Stats.Counter> original, int maxSize, int total) {
    List<Stats.Counter> sorted = sort(original);
    if (sorted.size() > maxSize) {
        int remainder = 0;
        for (Stats.Counter counter : sorted.subList(MAX_BAR_CHART_SIZE, sorted.size()))
            remainder += counter.count;//from w w w .  ja  va  2  s .  c om
        sorted = sorted.subList(0, MAX_BAR_CHART_SIZE);
        if (remainder > 0) {
            Stats.Counter counter = new Stats.Counter();
            counter.count = remainder;
            counter.value = "+";
            counter.setTotal(total);
            sorted.add(counter);
        }
    }
    return sorted;
}

From source file:com.android.dumprendertree2.FsUtils.java

public static void saveTestListToStorage(File file, int start, List<String> testList) {
    try {//ww  w.j av a  2  s . c  o  m
        BufferedWriter writer = new BufferedWriter(new FileWriter(file));
        for (String line : testList.subList(start, testList.size())) {
            writer.write(line + '\n');
        }
        writer.flush();
        writer.close();
    } catch (IOException e) {
        Log.e(LOG_TAG, "failed to write test list", e);
    }
}

From source file:com.trenako.results.PaginatedLists.java

/**
 * Returns a values page according the provided {@code Pageable}.
 *
 * @param values   the values to be paginated
 * @param paging the paging information//from w ww .  ja  va2  s . co m
 * @return a {@code Page}
 */
public static <T> Page<T> paginate(List<T> values, Pageable paging) {
    Assert.notNull(values, "Values list must be not null");

    int total = values.size();
    int offset = paging.getOffset();

    if (offset > total) {
        return failbackPage();
    }

    int sIndex = start(total, offset);
    int eIndex = end(total, offset, paging.getPageSize());

    List<T> content = values.subList(sIndex, eIndex);
    return new PageImpl<>(content, paging, total);
}