2016-04-05 32 views
0

我有3 List不同类型的Objects。 例如:Java分页列表

ListA包含64元素。

ListB包含33元素。

ListC包含515元素。

因此,总共有612元素。

我想让组(Pagination100元素,例如,它应该是这样的:

1ListA64元/ ListB33元/ ListC3元素

页面2ListA0 elements/ListB0元件/ ListC100元件

3ListA0元件/ ListB0元件/ ListC100元件

4ListA0元件/ ListB0元件/ ListC100元件

5ListA0元件/ ListB0元件/ ListC100元件

6ListA0元件/ ListB0 elements/ListC100元素

7ListA0元/ ListB0元/ ListC12元素

我的想法是创建一个Map<Integer, List<List>,其中key将是Pagevalue一个List其中包含3个Lists(每个ListABC)。

这里是我的代码:

  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); 
      } 

其中PAGE_SIZE是100

这是不工作,因为我当然需要检查多少元素每个list包含调用subListmethod之前。

我想我走错了路,但我没有看到另一种方式来做到这一点。

任何想法?

谢谢!

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

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; 
} 

谢谢大家的帮助!

回答

0

您可以将所有不同的对象放在一个List中,并使用instanceof来检测类的类型。

1

这不起作用,因为我当然需要在调用subList方法之前检查每个list包含多少个元素。

这似乎只是问题,如果你不愿意检查列表的大小。让至多大到整个列表子列表通过检查大小第一...

代替

listB.removeAll(listB.subList(0, (PAGE_SIZE-total)-1)); 

你也应该使用

listB.subList(0, (PAGE_SIZE-total)-1).clear(); 

防止相等的元素被移除偶然。

下面的方法提出了一个更加可重用的方式做任务,因为它不依赖于列表的具体数量,也不会修改源列表:从3独立List

转换输入数据结构s到包含这些列表的列表。这使您可以跟踪列表的索引以获取元素以及此列表中元素的第一个索引尚未使用。这样,您可以简单地浏览列表并添加项目,直到页面已满。

下面的代码返回一个List代替Map,因为键是数字0,...,N,但它可以很容易地修改,以返回地图:

public static <T> List<List<List<T>>> paginate(List<? extends List<? extends T>> objects, final int pageSize) { 
    List<List<List<T>>> result = new ArrayList<>(); 

    int index = 0; 
    int size = objects.size(); 

    // skip empty lists 
    while (index < size && objects.get(index).isEmpty()) { 
     index++; 
    } 

    for (int pageIndex = 0; index < size;) { 
     int remaining = pageSize; 
     List<List<T>> page = new ArrayList<>(size); 
     result.add(page); 
     for (int i = 0; i < index; i++) { 
      page.add(Collections.emptyList()); 
     } 
     while (remaining > 0 && index < size) { 
      List<? extends T> source = objects.get(index); 
      int lastIndex = Math.min(source.size(), pageIndex + remaining); 
      List<T> list = new ArrayList<>(source.subList(pageIndex, lastIndex)); 
      page.add(list); 
      remaining -= lastIndex - pageIndex; 
      if (lastIndex == source.size()) { 
       index++; 
       pageIndex = 0; 
       // skip empty lists 
       while (index < size && objects.get(index).isEmpty()) { 
        page.add(Collections.emptyList()); 
        index++; 
       } 
      } else { 
       pageIndex = lastIndex; 
      } 
     } 
     for (int i = page.size(); i < size; i++) { 
      page.add(Collections.emptyList()); 
     } 
    } 

    return result; 
} 
1

天真的建设每次都有一个全局列表(它的优点在于它始终使用底层列表的最后一个版本)。

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); 
    } 
} 

相关的测试类:

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()); 
    } 

}