要实现Java分页查询接口,可以按照以下步骤进行:
public interface PagingQueryService<T> {
List<T> queryByPage(int page, int pageSize);
}
public class PagingQueryServiceImpl<T> implements PagingQueryService<T> {
private List<T> dataSource; // 数据源,可以是数据库、文件等
public PagingQueryServiceImpl(List<T> dataSource) {
this.dataSource = dataSource;
}
@Override
public List<T> queryByPage(int page, int pageSize) {
int startIndex = (page - 1) * pageSize;
int endIndex = Math.min(startIndex + pageSize, dataSource.size());
return dataSource.subList(startIndex, endIndex);
}
}
public class Main {
public static void main(String[] args) {
List<String> dataSource = Arrays.asList("A", "B", "C", "D", "E", "F", "G", "H", "I", "J");
PagingQueryService<String> service = new PagingQueryServiceImpl<>(dataSource);
int page = 2;
int pageSize = 3;
List<String> result = service.queryByPage(page, pageSize);
System.out.println(result); // 输出:[D, E, F]
}
}
以上是一种简单的实现方式,根据具体需求可以进行适当的修改和扩展。