>  기사  >  Java  >  JAVA8 페이징 도구

JAVA8 페이징 도구

(*-*)浩
(*-*)浩앞으로
2019-09-19 16:30:093078검색

JAVA8 페이징 도구

JAVA8 API를 사용하면 페이징을 구현할 수 있습니다. 데이터의 양이 비교적 안정적이면 모든 데이터를 캐시로 찾아 사용할 수 있습니다

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
 
/**
 * @version 1.0
 * @author levelmini
 * @param <T>
 */
public class Page<T> {
	private int current_page;
	private int size;
	private int total_page; 
	private int total_sum;
	private transient List<T> instanceList;//Gson不序列化transient字段
	private List<T> currentPageData;
	private transient Optional<List<T>> op;
	
	public Page(List<T> instanceList,int size) {
		this.size = size;
		setInstanceList(instanceList);
	}
	
	public int getCurrent_page() {
		return current_page;
	}
	public void setCurrent_page(int current_page) {
		this.current_page = current_page<1?1:current_page>this.total_page?this.total_page:current_page;
		setCurrentPageData(currentPageData());
	}
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}
	public int getTotal_page() {
		return total_page;
	}
	public int getTotal_sum() {
		return total_sum;
	}
	public List<T> getInstanceList() {
		return instanceList;
	}
	public void setInstanceList(List<T> instanceList) {
		this.op= Optional.ofNullable(instanceList);
		this.instanceList = op.orElse(new ArrayList<T>());
		this.total_sum = this.instanceList.size();
		this.total_page =(int) Math.ceil(1.0*this.total_sum/this.size);
	}
	public void setCurrentPageData(List<T> currentPageData) {
		this.currentPageData = currentPageData;
	}
	public List<T> getCurrentPageData(){
		return this.currentPageData;
	}
	private List<T> currentPageData(){
		if(this.size==0 || this.total_page == 1){
			return this.instanceList;
		}
		List<T> currentPageData = new ArrayList<T>();
		instanceList.stream().skip((this.current_page-1)*this.size).limit(this.size).forEach(e->currentPageData.add(e));
		return currentPageData;
	}
}

사용법:

List<News> newsList = newsService.findAll();
Page<News> page = new Page<>(newsList, size);
page.setCurrent_page(current_page);
return JsonUtil.toJson(page,"yyyy-MM-dd");

위 내용은 JAVA8 페이징 도구의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 csdn.net에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제