Java 7 API design best practice - return Array or return Collection

Posted by Shengjie on Stack Overflow See other posts from Stack Overflow or by Shengjie
Published on 2012-11-29T10:47:02Z Indexed on 2012/11/29 11:04 UTC
Read the original article Hit count: 213

Filed under:
|

I know this question has be asked before generic comes out. Array does win out a bit given Array enforces the return type, it's more type-safe.

But now, with latest JDK 7, every time when I design this type of APIs:

public String[] getElements(String type)
vs
public List<String> getElements(String type)

I am always struggling to think of some good reasons to return A Collection over An Array or another way around. What's the best practice when it comes to the case of choosing String[] or List as the API's return type? Or it's courses for horses.

I don't have a special case in my mind, I am more looking for a generic pros/cons comparison.

© Stack Overflow or respective owner

Related posts about java

Related posts about collections