Search code examples
javaarraylistsize

How much data can a List can hold at the maximum?


How much data can be added in java.util.List in Java at the maximum?

Is there any default size of an ArrayList?


Solution

  • It depends on the List implementation. Since you index arrays with ints, an ArrayList can't hold more than Integer.MAX_VALUE elements. A LinkedList isn't limited in the same way, though, and can contain any amount of elements.

    Edit: I realize this sounds like an endorsement of LinkedList. But please note that although they don't have a theoretical capacity limit, linked lists are usually an inferior choice to array-based data structures because of higher memory use per element (which lowers the list's actual capacity limit) and poor cache locality (because nodes are spread all over RAM). It's definitely not the data structure you want if you have more items than can be crammed into an array.