Java分页列表 [英] Java Paginated List

查看:130
本文介绍了Java分页列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有不同类型的对象的3 列表
例如:



列表 A 包含 64 元素。



列表 B 包含 33 元素。



列表 C 包含 515 元素。



<总而言之,我有 612 元素。



我想要组(分页 100 元素,例如,它将如下所示:



1 列表 A 64 元素/ 列表 B 33 元素/ 列表 C 3 元素



2 列表 A 0 元素/ 列表 B 0 元素/ 列表 C 100 元素



3 列表 A 0 元素/ 列表 B 0 elements / 列表 C 100 元素



4 列表 A 0 元素/ 列表 B 0 元素/ 列表 C / code>元素



页面 5 列表 A 0 元素/ 列表 B 0 元素/ 列表 C 100 元素



6 列表 A 0 elements / 列表 B 0 元素/ 列表 C 100 元素



7 列表 A 0 元素/ 列表 B / code>元素/ 列表 C 12 e



我的想法是创建一个 Map< Integer,List< List> 其中密钥将是页面 a 列表其中包含3个列表(每个一个列表 A B C



这是我的代码:

  int totalPages = 0; 
int totalElements = listA.size()+ listB.size()+ listC.size();
if(totalElements%PAGE_SIZE == 0){
totalPages = totalElements / PAGE_SIZE;
} else {
totalPages =(totalElements / PAGE_SIZE)+ 1;
}


地图<整数,列表<父对象>>> paginatedMap = new HashMap< Integer,List< List< ParentObject>>>(); (int i = 0; i< totalPages; i ++)
{
列表< List< ParentObject>> list = new LinkedList< List< ParentObject>>();
列表< ObjectA> subListA = new LinkedList< ObjectA>();
列表< ObjectB> subListB = new LinkedList< ObjectB>();
列表< ObjectC> subListC = new LinkedList< ObjectC>();
int total = 0;

if(total< = PAGE_SIZE){
subListA.addAll(listA.subList(0,(PAGE_SIZE-total)-1));
listA.removeAll(listA.subList(0,(PAGE_SIZE-total)-1));
total = total + subListA.size();
}

if(total< = PAGE_SIZE){
subListB.addAll(listB.subList(0,(PAGE_SIZE-total)-1));
listB.removeAll(listB.subList(0,(PAGE_SIZE-total)-1));
total = total + subListB.size();
}

if(total< = PAGE_SIZE){
subListC.addAll(listC.subList(0,(PAGE_SIZE-total)-1));
listC.removeAll(listC.subList(0,(PAGE_SIZE-total)-1));
total = total + subListC.size();
}

list.add(subListA);
list.add(subListB);
list.add(subListC);
paginatedMap.put(i,list);
}

其中 PAGE_SIZE 100



这不起作用,因为我需要检查每个列表包含的元素在调用code> subList 方法



我觉得我错了方式,但我看不到另一种方式。



任何想法?



谢谢! / p>

最后我得到它的工作。
这里是代码:

  private Map<整数,列表< MyObject>>> paginateDataRequest(List< List< MyObject>> requestLists,double pageSize){
Map< Integer,List< List< MyObject>>> result = new LinkedHashMap< Integer,List< List< MyObject>>>();
int totalElements = 0;

//我们计算请求列表中包含的元素的总数。
for(List< MyObject> subList:requestLists){
if(subList!= null){
totalElements + = subList.size();
}
}

//我们圆整起来结果Map将包含x页面,每页都有{pageSize}个元素。例如,如果请求总数为101,则
//我们的Map将有2个页面(100个元素+ 1个元素)
int totalRequests =(int)Math.ceil(totalElements / pageSize);

//我们遍历每个页面
for(int i = 0; i< totalRequests; i ++){
列表< MyObject>> entry = new LinkedList< List< MyObject>>();

int freeElements =(int)pageSize;

for(List< MyObject> list:requestLists){
列表< MyObject> subList = new LinkedList< MyObject>();
if(freeElements> 0){
if(list.size()> freeElements){
subList.addAll(list.subList(0,freeElements));
} else {
subList.addAll(list);
}
//我们更新左边的空闲元素
freeElements - = subList.size();
}
entry.add(subList);
list.removeAll(subList);

}
//我们在结果中添加一个新页面
result.put(i,entry);
}

返回结果;
}

感谢大家帮忙!

解决方案

天真的解决方案每次构建一个全局列表(有利的是,它总是需要最后一个版本的底层列表)。

  public class ListPager {

public List< List< Object>> lists = new ArrayList< List< Object>>();

public int _pageSize = 100;

public void addList(List< Object> list){
lists.add(list);
}

public List< Object> getPage(int p){
列表< Object> global = new ArrayList< Object>();
for(List< Object> l:lists){
global.addAll(l);
}
if(p * _pageSize> global.size())return global;
int maxBound =(p + 1)* _ pageSize;
if(maxBound> global.size()){
maxBound = p * _pageSize +(global.size()%(p * _pageSize));
}
return global.subList(p * _pageSize,maxBound);
}
}

关联测试类:

  public class ListPagerTest {

public static class ObjectA {
public String toString(){returnA; }
}
public static class ObjectB {
public String toString(){returnB; }
}
public static class ObjectC {
public String toString(){returnC; }
}

@Test
public void test(){
列表< Object> listA = new ArrayList< Object>(); (int i = 0; i <64; i ++){listA.add(new ObjectA());
}
列表< Object> listB = new ArrayList< Object>(); (int i = 0; i <33; i ++){listB.add(new ObjectB());
}
列表< Object> listC = new ArrayList< Object>(); (int i = 0; i <515; i ++){listC.add(new ObjectC());
}

ListPager lp = new ListPager();
lp.addList(listA);
lp.addList(listB);
lp.addList(listC); (int i = 0; i< 7; i ++){
System.out.println(Page+ i);


print(lp.getPage(i));
}
}

public static void print(List< Object> l){
StringBuffer out = new StringBuffer();
for(Object o:l)out.append(o +,);
System.out.println(out.toString());
}

}


I have 3 List of different kind of Objects. For example:

List A contains 64 elements.

List B contains 33 elements.

List C contains 515 elements.

So in total, I have 612 elements.

I want to make groups (Pagination) of 100 elements, for example, it would look like this:

Page 1: List A: 64 elements / List B: 33 elements / List C: 3 elements

Page 2: List A: 0 elements / List B: 0 elements / List C: 100 elements

Page 3: List A: 0 elements / List B: 0 elements / List C: 100 elements

Page 4: List A: 0 elements / List B: 0 elements / List C: 100 elements

Page 5: List A: 0 elements / List B: 0 elements / List C: 100 elements

Page 6: List A: 0 elements / List B: 0 elements / List C: 100 elements

Page 7: List A: 0 elements / List B: 0 elements / List C: 12 elements

My idea is creating a Map<Integer, List<List> where the key would be the Page and the value a List which contains 3 Lists (one for each List A, B or C).

Here is my code:

            int totalPages = 0;
            int totalElements = listA.size() + listB.size() + listC.size();
            if(totalElements % PAGE_SIZE == 0) {
                totalPages = totalElements / PAGE_SIZE;
            }else {
                totalPages = (totalElements / PAGE_SIZE) + 1;
            }


            Map<Integer, List<List<ParentObject>>> paginatedMap = new HashMap<Integer, List<List<ParentObject>>>();
            for(int i=0; i<totalPages; i++) {
                List<List<ParentObject>> list = new LinkedList<List<ParentObject>>();
                List<ObjectA> subListA = new LinkedList<ObjectA>();
                List<ObjectB> subListB = new LinkedList<ObjectB>();
                List<ObjectC> subListC = new LinkedList<ObjectC>();
                int total = 0;

                if(total <= PAGE_SIZE) {
                    subListA.addAll(listA.subList(0, (PAGE_SIZE-total)-1));
                    listA.removeAll(listA.subList(0, (PAGE_SIZE-total)-1));
                    total = total + subListA.size();
                }

                if(total <= PAGE_SIZE) {
                    subListB.addAll(listB.subList(0, (PAGE_SIZE-total)-1));
                    listB.removeAll(listB.subList(0, (PAGE_SIZE-total)-1));
                    total = total + subListB.size();
                }

                if(total <= PAGE_SIZE) {
                    subListC.addAll(listC.subList(0, (PAGE_SIZE-total)-1));
                    listC.removeAll(listC.subList(0, (PAGE_SIZE-total)-1));
                    total = total + subListC.size();
                }

                list.add(subListA);
                list.add(subListB);
                list.add(subListC);
                paginatedMap.put(i, list);
            }

where PAGE_SIZE is 100

That's not working because of course I need to check how many elements each list contains before calling the subList method.

I think I'm taking the wrong way, but I don't see another way to do it.

Any ideas?

Thanks!

Finally I got it to work. Here is the code:

private Map<Integer, List<List<MyObject>>> paginateDataRequest(List<List<MyObject>> requestLists, double pageSize) {
    Map<Integer, List<List<MyObject>>> result = new LinkedHashMap<Integer, List<List<MyObject>>>();
    int totalElements = 0;

    //We calculate the total of the elements contained in the requestLists.
    for(List<MyObject> subList : requestLists) {
        if(subList != null) {
            totalElements += subList.size();
        }
    }

    //We round it up. The result Map will contain x pages with {pageSize} elements each one. For example, if the total amount of request is 101,
    //our Map will have 2 pages (100 elements + 1 element)
    int totalRequests = (int)Math.ceil(totalElements / pageSize);

    //We iterate over each page
    for(int i=0; i<totalRequests; i++) {
        List<List<MyObject>> entry = new LinkedList<List<MyObject>>();

        int freeElements = (int)pageSize;

        for(List<MyObject> list : requestLists) {
            List<MyObject> subList = new LinkedList<MyObject>();
            if(freeElements > 0) {
                if(list.size() > freeElements) {
                    subList.addAll(list.subList(0, freeElements));
                }else {
                    subList.addAll(list);
                }
                //We update the left free elements
                freeElements -= subList.size();
            }
            entry.add(subList);
            list.removeAll(subList);

        }
        //We add a new page to the result Map
        result.put(i, entry);
    }

    return result;
}

Thanks everyone for helping!

解决方案

Naive solution building a global list each time (the advantege beeing that it always takes the last version of underlying lists).

public class ListPager {

    public List<List<Object>> lists = new ArrayList<List<Object>>();

    public int _pageSize = 100;

    public void addList(List<Object> list) {
        lists.add(list);
    }

    public List<Object> getPage(int p) {
        List<Object> global = new ArrayList<Object>();
        for (List<Object> l : lists) {
            global.addAll(l);
        }
        if (p*_pageSize > global.size()) return global;
        int maxBound = (p + 1)*_pageSize;
        if (maxBound > global.size()) {
            maxBound = p*_pageSize + (global.size()%(p*_pageSize));
        }
        return global.subList(p*_pageSize, maxBound);
    }
}

associated test class:

public class ListPagerTest {

    public static class ObjectA {
        public String toString() { return "A"; }
    }
    public static class ObjectB {
        public String toString() { return "B"; }
    }
    public static class ObjectC {
        public String toString() { return "C"; }
    }

    @Test
    public void test() {
        List<Object> listA = new ArrayList<Object>();
        for (int i = 0 ; i < 64 ; i++) { listA.add(new ObjectA()); }
        List<Object> listB = new ArrayList<Object>();
        for (int i = 0 ; i < 33 ; i++) { listB.add(new ObjectB()); }
        List<Object> listC = new ArrayList<Object>();
        for (int i = 0 ; i < 515 ; i++) { listC.add(new ObjectC()); }

        ListPager lp = new ListPager();
        lp.addList(listA);
        lp.addList(listB);
        lp.addList(listC);

        for (int i = 0 ; i < 7 ; i++) {
            System.out.println("Page " + i);
            print(lp.getPage(i));
        }
    }

    public static void print(List<Object> l) {
        StringBuffer out = new StringBuffer();
        for (Object o : l) out.append(o + ",");
        System.out.println(out.toString());
    }

}

这篇关于Java分页列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆