Java中list集合分页实现

/**
 * 
  * 开始分页
 * @param list
 * @param pageNum  页码
 * @param pageSize 每页多少条数据
 * @return
 */
public  List<T> startPage(List<T> list, Integer pageNum, Integer pageSize) {
	if (list == null||list.size() == 0) {
		return new ArrayList<T>(0);
	}

	Integer count = list.size(); // 记录总数
	Integer pageCount = 0; // 页数
	if (count % pageSize == 0) {
		pageCount = count / pageSize;
	} else {
		pageCount = count / pageSize + 1;
	}

	int fromIndex = 0; // 开始索引
	int toIndex = 0; // 结束索引

	if (pageNum != pageCount) {
		fromIndex = (pageNum - 1) * pageSize;
		toIndex = fromIndex + pageSize;
	} else {
		fromIndex = (pageNum - 1) * pageSize;
		toIndex = count;
	}
	List<T> pageList = list.subList(fromIndex, toIndex);
	return pageList;
}
发布了81 篇原创文章 · 获赞 5 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_36205206/article/details/103456033