Java memory paging Tools

Foreword

Course of their work, often encounter the situation paged memory-based data processing, for example, batch updates the database, a collection of cases is too large batch updates require, for example, there is a collection of data to cache paging get this case.
This article provides general-purpose memory paging tools, reference some code on the network, mainly based subList () method to achieve, I hope for your help! Source tools at the bottom of this article. Original article link address: http: //nullpointer.pw/Java%E5%86%85%E5%AD%98%E5%88%86%E9%A1%B5%E5%B7%A5%E5%85%B7 % E7% B1% BB.html

Optimization ago

First look normal if you want to fulfill these requirements, then, is how to implement the code of

// 分页进行批量更新
private void batchUpdateStudent(List<Student> students) {
    int limit = 100;
    int size = students.size();
    int m = size / limit;
    int n = size % limit;
    for (int i = 1; i <= m; i++) {
        List<Student> list = students.subList((i - 1) * limit, i * limit);
        studentDao.batchUpdate(list);
    }
    if (n != 0) {
        List<Student> list = students.subList(m * limit, students.size());
        studentDao.batchUpdate(list);
    }
}

// 分页获取数据
public List<Student> pageStudents(Integer page, Integer pageSize) {
    if (page < 1) {
        page = 1;
    }

    int start = (page - 1) * pageSize;
    int limit = page * pageSize;

    // 从缓存中获取全量数据
    List<Student> students = studentCache.getStudents();
    if (CollectionUtils.isEmpty(students)) {
        return new ArrayList<>();
    }
    if (limit > students.size()) {
        limit = students.size();
    }
    return students.subList(start, limit), students.size();
}

Code can be seen that the method of comparison of redundancy, if need multiple memory paging, code duplication is inevitable there will be many!

Optimized

// 分页进行批量更新
private void batchUpdateStudent(List<Student> students) {
    RAMPager<Student> pager = new RAMPager<>(students, 100);
    // 方式一:使用迭代器
    Iterator<List<Student>> iterator = pager.iterator();
    while (iterator.hasNext()) {
        studentDao.batchUpdate(iterator.next());
    }

    // 方式二:使用索引
    //for (int i = 1; i <= pager.getPageCount(); i++) {
    //  studentDao.batchUpdate(pager.page(i));
    //}
}

// 分页获取数据
public List<Student> pageStudents(Integer page, Integer pageSize) {
    // 从缓存中获取全量数据
    List<Student> students = studentCache.getStudents();
    RAMPager<Student> pager = new RAMPager<>(students, pageSize);
    return pager.page(page);
}

Note: If only paging, without the need for attention to page number, you can use an iterator;

Source Tools

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * 类名称:RAMPager
 * 类描述:内存分页工具
 * 创建人:WeJan
 * 创建时间:2019年07月22日 13:43
 * Version: 1.1
 */
public class RAMPager<T> {
    private List<T> data;
    private int pageSize;

    /**
     * @param data     原始数据
     * @param pageSize 每页条数
     */
    public RAMPager(List<T> data, int pageSize) {
        this.data = data;
        this.pageSize = pageSize;
    }

    /**
     * 获取某页数据,从第1页开始
     *
     * @param pageNum 第几页
     * @return 分页数据
     */
    public List<T> page(int pageNum) {
        if (pageNum < 1) {
            pageNum = 1;
        }
        int from = (pageNum - 1) * pageSize;
        int to = Math.min(pageNum * pageSize, data.size());
        if (from > to) {
            from = to;
        }
        return data.subList(from, to);
    }

    /**
     * 获取总页数
     */
    public int getPageCount() {
        if (pageSize == 0) {
            return 0;
        }
        return data.size() % pageSize == 0 ? (data.size() / pageSize) : (data.size() / pageSize + 1);
    }

    /**
     * 元素迭代器
     */
    public Iterator<List<T>> iterator() {
        return new Itr();
    }

    private class Itr implements Iterator<List<T>> {
        int page = 1;

        Itr() {
        }

        public boolean hasNext() {
            return page <= getPageCount();
        }

        public List<T> next() {
            int i = page;
            if (i > getPageCount())
                return new ArrayList<>();

            page = i + 1;
            return RAMPager.this.page(i);
        }
    }

    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
        System.out.println("原始数据是:" + list);

        int pageSize = 2;
        System.out.println("每页大小是:" + pageSize);

        RAMPager<Integer> pager = new RAMPager<>(list, pageSize);
        System.out.println("总页数是: " + pager.getPageCount());

        System.out.println("<- - - - - - - - - - - - - ->");

        // 无需感知页码情况下使用
        Iterator<List<Integer>> iterator = pager.iterator();
        while (iterator.hasNext()) {
            List<Integer> next = iterator.next();
            System.out.println("next: " + next);
        }

        System.out.println("<- - - - - - - - - - - - - ->");
        // 需要指定页码情况使用,页码从第一页开始,且小于等于总页数!
        for (int i = 1; i <= pager.getPageCount(); i++) {
            List<Integer> page = pager.page(i);
            System.out.println("第 " + i + " 页数据是:" + page);
        }
    }
}

Epilogue

I hope useful for you ~

Guess you like

Origin www.cnblogs.com/vcmq/p/12149654.html