私は、Objects
の3種類のList
を持っています。 例:Javaページ番号リスト
List
A
には、64
要素が含まれています。
List
B
は、33
要素を含んでいます。
List
C
は、515
要素を含んでいます。
合計で、私は612
要素を持っています。私は100
要素のグループ(Pagination
)を作りたい
は、例えば、それは次のようになります。
ページ1
:List
A
:64
要素/ List
B
:33
要素/ List
C
:3
要素を
ページ2
:List
A
:0
要素/ List
B
:0
要素/ List
C
:100
要素
ページ3
:List
A
:0
要素/ List
B
:0
要素/ List
C
:100
要素
ページ4
:List
A
:0
要素/ List
B
:0
要素/ List
C
:100
要素
ページ5
:List
A
:0
要素/ List
B
:0
要素/ List
C
:100
要素
ページ6
:List
A
:0
要素/ List
B
。 0
要素/ List
C
:100
要素
ページ7
:List
A
:0
要素/ List
B
:0
要素/ List
C
:12
要素
私の考えはMap<Integer, List<List>
key
は次のようになりどこPage
とvalue
作成されますList
は、3つのLists
(各1つにつき、List
A
,B
またはC
)。 PAGE_SIZE
はもちろん、私はどのように多くの要素をチェックする必要があるため働いていない100
ある
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);
}
がそれぞれlist
がsubList
method
を呼び出す前に含まれています
は、ここに私のコードです。
私は間違った方法を取っていると思いますが、それを行う別の方法はありません。
アイデア?
ありがとうございます!
最後に私はそれを働かせました。
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;
}
みんなありがとう助けるために:ここ は、コードです!