Return ArrayList or List?

java, list, optimization

Solution

Returning List will make it possible for users of your library to use anything that implements a List interface, while using an ArrayList will force them to use an ArrayList.

If in the future, you as a library creator decide to change your internal implementation, that changes will be hidden from the end user by the generic interface to your library.

Problem

I'm creating a library to be used by people but why should my method return a List instead of an ArrayList? Because if the user knows the real type is an ArrayList he will use the accessor [] in a loop instead of iterator but if he doesn't know he will use an iterator. Same question for LinkedList, if the return type is a List he won't be able to use the correct accessor. Am I right?

Original source

Related problems