Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures Exploring the Java Collection Framework Lists

Arnold Rosario
Arnold Rosario
16,357 Points

What's an example of changing the implementation whenever we want?

So in regards to using the interface List<String> as the type on the declaration side and using the implementation ArrayList<String>(); on the right side to allow us to change the implementation at any time.

What would be an example of doing that?

How come I see examples of people doing ArrayList<String> todoList = ArrayList<String>();?

Is List<String> todoList = ArrayList<String>(); the better practice?

1 Answer

Charles Cloud
Charles Cloud
6,134 Points

I think this link may be helpful to you: https://stackoverflow.com/questions/4062982/list-versus-arraylist-as-reference-type

Per my limited understanding it seems that it varies depending on the operations that you will need to do with the data. If you know that you need to use a specific type of operation (that is only available on ArrayList for instance) on the data then you can be specific. If not, then leave it open and you are afforded greater flexibility in how you can use the data. The flexibility comes in being able to go back into your code later and change the implementation without breaking code that depends on simply the methods that list has as opposed to ones specific to implementations.