Return Arraylist or List?

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?

4

8 Answers

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.

10

Because the users of your library should never know that you are using an ArrayList internally.

For example, say you return an ArrayList and lots of people have started using your library. Now if you suddenly realize a LinkedList better suits your purpose, then you break compatibility for all the folks who are presently using your code.

This is why it is always better to code to an interface, not an implementation, and even more so when you are writing code that is specifically meant to be re-used. The interface (List in this case) acts as a contract between your code and the client's code. It tells them what your code does (interface), without telling them how it does it (by not exposing the implementation).

Return an interface (or failing that a super class?) if possible. This way the method can have a broader application if overriden. This might prevent some class-specific methods from being available on the returned object but there's nothing stopping a programmer taking the List data and copying it to whatever Collection they prefer to use.

List myList = new MyLibrary().getList();

ArrayList myArrayList = new ArrayList(myList);
LinkedList myLinkedList = new LinkedList(myList);
1

They don't have to use an iterator - the List interface supports get(int index) as a method. If you want flexibility to change the underlying container to anything supporting the list interface, use List. If specific ArrayList methods are required on what you return, use ArrayList.

Because your user can make from your List either ArrayList or LinkedList, you will leave him a choice. It's called Programming to Interface. You should give users of your API as much freedom as you can and this technique is one of the ways how to achieve it.

1. Its the concept of Interface Polymorphism.

2. Its better to have List<My_Obj> arr = new ArrayList<My_Obj>;

3. Suppose you want to use LinkedList instead of ArrayList as somepoint, then you donot need to worry abt how to do it..

If You are returning List then it is possible for users of your library to use anything that implements a List interface. It may be Array List or Linked List.

I typically choose the most general type possible. In this case, you could return a type that's even more general than a List, such as Collection or Iterable.

By returning Iterable, the compiler will prevent the calling code from attempting to add elements to your list. This is much stronger than relying on Collections.unmodifiableList() to fail at runtime.

Using more general types also gives you more room to manoeuvre in the future. Perhaps your method's going to load your data from a streaming source rather than an in-memory source: then Iterable becomes a much more suitable than List.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest