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 Using ArrayLists

Rohit Patwari
Rohit Patwari
5,500 Points

Why is getWords return type changed to List from Arrays

getWords() in Treet class was returning Array . Why was it changed to Array.asList() . What is the practical significance of that . In the Video it has been said that it is done to make the return types uniform . What does that actually mean ?

2 Answers

Christopher Augg
Christopher Augg
21,223 Points

List is an interface. It can hold a reference to different types of List implementations.

The Arrays Class provides methods for manipulating arrays. The asList() method returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)

The ArrayList class is a resizable-array implementation of the List interface.

Therefore, these are not the same types of List objects.

I think Dennis used Arrays.asList() in his video example to get a fixed-length list that could be referenced as a List type. A developer using his class really should not be able to add words to a list from a getter method. Of course, a developer should not be able to set words either, but a list from Arrays.asList() allows that. Furthermore, encapsulation violations are easily committed by having public methods that just return array references. This is because the array is now accessible even after the method call where it should be out of scope. It is especially true when the array is a private member variable of the class. It is better to place the words into a new ArrayList and return it. This prevents encapsulation violation and provides uniformity.

Arrays.asList() returns a list that is not uniform with the other methods even though it is being held in a List interface as a reference. A List interface type can hold a reference to the fixed size list that Arrays.asList() returns. It is not completely uniform though because the other methods returned are ArrayLists. One can return an actual arrayList type though:

public List<String> getWords() {
    String[] words = mDescription.toLowerCase().split([^\\w#@‘]+);
    ArrayList<String> wordList = new ArrayList<String>(Arrays.asList(words));
    return wordList;
}

This would be more uniform with the other methods because they would all now have the same functionality.

I hope this helps.

Regards,

Chris

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,610 Points

I found a post see if it helps you understand the comparison between Arrays.asList() and a new ArrayList()

As far as keep the return types uniform. If you keep return types the same on all your methods, then when you call on those methods later from other classes or other methods you already know ahead of time what TYPE of data you are getting back. You know it will be formatted to a certain type of List<String> or such. This way you dont have to jump around so much trying to remember what something is being returned as.

Rohit Patwari
Rohit Patwari
5,500 Points

Hey Chris . Thanks . The link you provided is pretty useful : ) .