Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {

    public static <T> List<T> pageList(List<T> list, int page, int size) {
        if (page < 1) {
            throw new IllegalArgumentException("page should not little than 1,page:" + page);
        }
        if (size < 1) {
            throw new IllegalArgumentException("size should not little than 1,size:" + size);
        }
        if (isEmpty(list)) {
            return Collections.emptyList();
        }
        int totalSize = list.size();

        int fromIndex = (page - 1) * size > (totalSize - 1) ? 0 : (page - 1) * size;

        int endIndex = (fromIndex + size) > (totalSize) ? (totalSize) : (fromIndex + size);

        return list.subList(fromIndex, endIndex);

    }

    public static <T> boolean isEmpty(List<T> list) {
        return list == null || list.isEmpty();
    }

    public static <K, V> boolean isEmpty(Map<K, V> map) {
        return map == null || map.isEmpty();
    }
}