list集合实现分页和模糊查询

因为特殊原因,有些前端列表需要展示的内容比较复杂,可能是合并了几个集合后最终展示的,所以此时就无法使用pagehelper分页,之前在网上找了一些工具类,自己也稍微改造了下
**

  • list集合工具类
    /
    public class ListUtils {
    /
    *

    • @param pageSize 当前页面大小

    • @param pageIndex 当前页码

    • @param list 需要分页的集合

    • @return
      */
      public static List Pager(int pageSize, int pageIndex, List list) {
      //使用list 中的sublist方法分页
      List dataList = new ArrayList();
      // 每页显示多少条记录

      int currentPage; //当前第几页数据

      int totalRecord = list.size(); // 一共多少条记录

      int totalPage = totalRecord % pageSize; // 一共多少页
      if (totalPage > 0) {
      totalPage = totalRecord / pageSize + 1;
      } else {
      totalPage = totalRecord / pageSize;
      }

      System.out.println(“总页数:” + totalPage);

      // 当前第几页数据
      currentPage = totalPage < pageIndex ? totalPage : pageIndex;

      // 起始索引
      int fromIndex = pageSize * (currentPage - 1);

      // 结束索引
      int toIndex = pageSize * currentPage > totalRecord ? totalRecord : pageSize * currentPage;
      try {
      if (list.size() > 0) {
      dataList = list.subList(fromIndex, toIndex);
      }
      } catch (IndexOutOfBoundsException e) {
      e.printStackTrace();
      }
      return dataList;
      }

    //执行模糊查询
    public static List seach(List list, String title) {
    List results = new ArrayList();
    Pattern pattern = Pattern.compile(title);
    for (int n = 0; n < list.size(); n++) {
    Matcher matcher = pattern.matcher(((ProposalInfo) list.get(n)).getTitle());
    if (matcher.find()) {
    results.add(list.get(n));
    }
    }
    return results;
    }

}

可以直接调用实现分页跟模糊查询

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值