Get circle Sub List - Java java.util

Java examples for java.util:List Sub List

Description

Get circle Sub List

Demo Code


//package com.java2s;

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

public class Main {
    public static void main(String[] argv) {
        List srcList = java.util.Arrays.asList("asdf", "java2s.com");
        int position = 42;
        int count = 42;
        System.out.println(circleSubList(srcList, position, count));
    }/*  w w w.j a v  a 2s . co  m*/

    public static <T> List<T> circleSubList(List<T> srcList, int position,
            int count) {
        if (srcList == null || srcList.size() == 0) {
            return Collections.emptyList();
        }
        position = position < 0 ? 0 : position;
        count = count > srcList.size() ? srcList.size() : count;
        if (position > srcList.size() - 1) {
            return srcList.subList(0, count);
        }
        if (position + count <= srcList.size()) {
            return srcList.subList(position, (position + count));
        }
        List<T> fList = srcList.subList(position, srcList.size());
        List<T> eList = srcList.subList(0, count - srcList.size()
                + position);
        fList.addAll(eList);
        return fList;
    }
}

Related Tutorials