Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Understanding Collections.sort()

 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you pass in a list to Collections.sort(), it sorts your list and points your variable to that sorted list. I don't understand exactly how it works. In other words, how is it that the sort method is void? I would expect to have to do list = Collections.sort(list);.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think a new list is created? Collections.sort() is sorting the list that gets passed in.

The JavaDoc actually notes that the list passed in must be modifiable. This is why. sort() needs to be able to change it to sort it.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I thought that methods used a copy of whatever was passed in and that if changes are made to the object in the method, no changes are made to the original object. So my assumption is false, I take it?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's only true for primitives. The following is valid code that changes the Foo object.



 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then explain the below code. It prints out "unchanged" even though Strings are objects.

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't change the value of s. You assigned it to a new value. That's equivalent to



Whereas in my original example, I changed an instance variable on the Foo object. It's the same foo object. This example is more equivalent to sort because sort is changing the list passed in.

 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so then how would I change String object h from a method?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You wouldn't. String objects are special in that they are final. Which means they are immutable and you can't call any methods on them that will change state.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, thanks for the help! Here was what I did, which successfully printed out -1:

 
Winston Gutkowski
Bartender
Pie
Posts: 10111
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel wrote:In other words, how is it that the sort method is void? I would expect to have to do list = Collections.sort(list);.

I think Jeanne's basically explained it to you, but you know what? I totally agree with you. I wish there was a version that also returned the List for you (sortAndReturn?). Indeed, I've written one for myself.
I also tend to write my setters that way too (ie, return a copy of the object that contains the method). Guess I just don't like void methods very much.

The style is used extensively with fluid interfaces, which are quite interesting; but you have to be careful - method chaining can produce some horrible code.

Winston
 
Campbell Ritchie
Sheriff
Posts: 48441
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect part of the reason for using such output parameters is historical. Did they do it that way in C++?
 
Matthew Brown
Bartender
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's another possible justification (which you may or may not agree with). Returning void makes it clear that the sort() method has side-effects (since a void method that doesn't is pointless) - so it should be clear that it sorts the list that you passed in. If it returned the list, that might suggest that it's returning a new sorted list, leaving the old one alone.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic