Java List Sub List subList(List list, int skip, int top)

Here you can find the source of subList(List list, int skip, int top)

Description

Returns a subList.

License

Open Source License

Parameter

Parameter Description
E type of element in list
list to partition
skip or first index
top maximum number of element in the sublist to return

Return

a non null sublist (may be empty)

Declaration

public static <E> List<E> subList(List<E> list, int skip, int top) 

Method Source Code


//package com.java2s;
/*/*from  w w  w .ja v a2 s  .c  o m*/
 * Data Hub Service (DHuS) - For Space data distribution.
 * Copyright (C) 2018 GAEL Systems
 *
 * This file is part of DHuS software sources.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.Collections;
import java.util.List;

public class Main {
    /**
     * Returns a subList.
     * Does not throw OutOfBounbsException if skip or top is too high.
     *
     * @param <E> type of element in list
     * @param list to partition
     * @param skip or first index
     * @param top maximum number of element in the sublist to return
     * @return a non null sublist (may be empty)
     */
    public static <E> List<E> subList(List<E> list, int skip, int top) {
        // skipped everything
        if (top == 0 || skip >= list.size()) {
            return Collections.emptyList();
        }

        // avoid OOB exception
        int lastIndex = skip + top;
        if (lastIndex > list.size()) {
            lastIndex = list.size();
        }

        return list.subList(skip, lastIndex);
    }
}

Related

  1. sublist(LinkedHashSet base, int start, int count)
  2. subList(List list, int start, int end)
  3. subList(List list, int fromIndex, int toIndex)
  4. subList(List list, int page, int size)
  5. subList(List list, int fromIndex)
  6. subList(List list, int start, int end)
  7. subList(List list, int begin, int end)
  8. subList(List lst, Collection indices)
  9. subList(List initial, int threshold)