Is it better to use some of thread safe collections ? Scope of the question is only the generation and adding of elements into the list.
public List<BankDetailsDTO> generateBankDetailsDTOs(int amount) {
List<BankDetailsDTO> result = new ArrayList<>();
IntStream.range(0, amount).parallel().forEach(index -> {
result.add(new BankDetailsDTO(bankDetailsDTOCounter.getAndIncrement(),
faker.company().name(),
faker.finance().iban(),
faker.finance().bic(),
faker.finance().iban()));
});
return result;
}
}
Update Ok this was an bad idea.
public List<BankDetailsDTO> generateBankDetailsDTOs(int amount) {
List<BankDetailsDTO> result = Collections.synchronizedList(new ArrayList<>());
IntStream.range(0, amount).parallel().forEach(index -> {
result.add(new BankDetailsDTO(bankDetailsDTOCounter.getAndIncrement(),
faker.company().name(),
faker.finance().iban(),
faker.finance().bic(),
faker.finance().iban()));
});
return result;
}