First and foremost– I have a file of strings. The smallest file is about 20 strings. The largest file is currently 12,000 strings of varying lengths (anywhere from one character to about 80). I suspect I may have up to a 60,000 string file in the future.
Initially I made a standard array of strings with a default size of 200 and doubled the size and copied the array to a new array if needed (while reading the file into the array). This method was pretty fast. However, the readability and extra coding for methods like search or contains was not appealing. I tried a List interface instead– and read the file in using the typical list.add(line) until there were no more lines.
My question is: What is the default size of an ArrayList<> and does this method result in too many allocations/resizes? Is there any performance points I should know about these two methods and which would be better?
ArrayList defaults to size 10. The amortized cost is not very expensive, even if you start with size 1. You could turn the cost down to nearly 0 if you initialize it with a high capacity:
Also, you should realize that the
Listinterface doesn’t intrinsically have any performance standards. Its implementations likeLinkedListandArrayListdo.Edit: I’m lazy and would never use a straight array.
ArrayListpretty much is the array with all of the functions likeadd()andremove()built in. The traditional list implementation, theArrayList, is the alternative that I would usually consider, but if you are going to be searching the thing I’d suggest sorting it once after you’re done loading it, and using anArrayListto make use of that with binary search.