I get Arraylist of object of say 1000 elements and i want to create batch of 100.
How to do it in java 8 in some elegant way?
I have following entity to iterate upon which has size of 1000:
List<CustomerAgreement> customerAgreement
Now i will call following methods after above
customerAgreementDao.createAll(customerAgreement);
customerAgreementDao.flush();
How can i create batches from above entity and call above two methods in that batch?
Current Standard way of doing this is somewhat like:
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<888888; i++ ) {
TableA record = new TableA();
record.setXXXX();
session.save(record)
if ( i % 50 == 0 ) {
session.flush();
session.clear();
}
}
tx.commit();
session.close();
I'd myself use List.subList
, as I'm not so into lambda's; they generally make your code less readable in my opinion.
This should work - if you don't mind I've scaled back on the array:
// create list as demo
var list = new ArrayList<String>();
for (int i = 0; i < 13; i++) {
list.add(Integer.toString(i));
}
int batchSize = 3;
// the code
int offset = 0;
while (offset < list.size()) {
// make sure the end offset doesn't go past the end
int endOffset = Math.min(offset + batchSize, list.size());
// call or add it to anything else, or even use streaming afterwards
System.out.println(list.subList(offset, endOffset));
offset = endOffset;
}
results in
[0, 1, 2]
[3, 4, 5]
[6, 7, 8]
[9, 10, 11]
[12]
Beware that the sublists are not copies, any changes to the objects in the list will be reflected in the sub lists, while structural changes (resizing) to the original list will result, well, probably in a mess. This also goes for the other way around, although structural changes to the subList
are possible.