• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Understanding Collections.sort()

 
Ranch Hand
Posts: 250
1
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then explain the below code. It prints out "unchanged" even though Strings are objects.

 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so then how would I change String object h from a method?
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, thanks for the help! Here was what I did, which successfully printed out -1:

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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++?
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic