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();
public void consumerTest() { AtomicInteger ai = new AtomicInteger(0); IntStream.range(0, 100).boxed().map(Table::new) .forEach(r -> this.forEachConsumer(r, ai.addAndGet(1))); } public void forEachConsumer(Table table, Integer counter) { System.out.println(counter); if (counter % 30 == 0) { System.err.println(counter); } }
and I will not recommend this code inside production – Dichotomy