jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Understanding Collections.sort() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Understanding Collections.sort()" Watch "Understanding Collections.sort()" New topic
Author

Understanding Collections.sort()

Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 231
    
    1

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

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.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 231
    
    1

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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

That's only true for primitives. The following is valid code that changes the Foo object.



Joel Christophel
Ranch Hand

Joined: Apr 20, 2011
Posts: 231
    
    1

Then explain the below code. It prints out "unchanged" even though Strings are objects.

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

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

Joined: Apr 20, 2011
Posts: 231
    
    1

Ok, so then how would I change String object h from a method?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

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

Joined: Apr 20, 2011
Posts: 231
    
    1

Okay, thanks for the help! Here was what I did, which successfully printed out -1:

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38075
    
  22
I suspect part of the reason for using such output parameters is historical. Did they do it that way in C++?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4344
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Understanding Collections.sort()
 
Similar Threads
collection.sort() explanation
Mr. Bloch - Java "conductive to quality and prductivity, and a joy to work with"
how come Set is not sortable
Hibernate sorting issue
ArrayList question