Save ArrayList or LinkedList is faster in JPA?

Viewed 59

LinkedList is faster in add element. But ArrayList is better in stored data.

I suppose that I will have 1 million elements to add to the List. Then I will use method saveAll() to save them in DB. Code like below:

//ArrayList
List<Person> personList = new ArrayList<>();
fullPersonList.forEach(item -> {
    if (item.isMale())
        personList.add(item);
});
personRepository.saveAll(personList);

//LinkedList
List<Person> personList = new LinkedList<>();
fullPersonList.forEach(item -> {
    if (item.isMale())
        personList.add(item);
});
personRepository.saveAll(personList);
1 Answers

It would be better seeing the definition of personRepository.saveAll(personList)

Regardless, ArrayList internally uses an array. I expect a read operation to be hence faster, so I'd go with the ArrayList

see linked_list_implementation here

Related