Java – convert the list to a list of 10 items
•
Java
I have a list of POJOs Convert this POJOs list to a list, where each sublist is 10 or smaller in size So, for example, convert a list of size 13 to a two element list The first element is a list of 10 items, and the second element is a list of 3 items
So the data structure is list < list < POJO > >
To create this list:
List<List<pojo>> pojoList counter = 0; initialise new tempList iterate list add current pojo to temp list if counter = 10 then add tempList to pojoList reset counter and tempList and continue until list is iterated
Is there an alternative solution?
Solution
Use sublist
List<Pojo> originalList.... //your list of POJOs List<List<Pojo>> pojoList = new ArrayList<List<Pojo>>(originalList/10 + 1); for(int i = 0; i < originalList.size(); i+=10){ if(i + 10 > originalList.size()){ pojoList.add(originalList.subList(i,originalList.size())); } else{ pojoList.add(originalList.subList(i,i + 10)); } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码