File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Sorting question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Sorting question" Watch "Sorting question" New topic
Author

Sorting question

Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Is there any difference between the static methods Arrays.sort() and Collections.sort()?

Why would I use one over the other? They seem to do the exact same thing, so why are there 2 of them?

Thank you!


SCJA
~Currently preparing for SCJP6
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Brian Arrays.sort is used to sort arrays and Collections.sort is used to sort Lists. How can you compare them. You cannot say

int[] arr = ...;
Collections.sort(arr); //error


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
patrick avery
Ranch Hand

Joined: Sep 12, 2008
Posts: 46
I believe that Arrays.sort only handles array objects (arrays of primitives or objects) while Collections sort only handles List objects.


SCJA 96%
SCJP 6 88%
skipping SCJD to work on passing SCWCD
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
*goes and hides in corner*

I can't believe I missed that this whole time. I never ever ever use arrays, only classes in the collection api so I never even thought of that.

Thank you!
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
On a side note.... since Collections.sort() takes a list then I assume if you need to sort a HashMap for example then you would have to use a method to grab all the keys or values as a List and pass that?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

if you want to sort a Map or a Set, you use TreeMap and TreeSet respectively. Just use the overloaded constructor as follows

new TreeMap(hashMap);

new TreeSet(hashSet);

I have used the non-generic versions here, but you must use the generic versions of these classes
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Of course....

Thanks again Ankit!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13868
    
  10

There are also LinkedHashMap and LinkedHashSet, which keep the elements in the order that you put them into the map or set.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Yes, but that would be ordered, not sorted
Lidija Sketa
Greenhorn

Joined: Nov 12, 2008
Posts: 26
HashMap doesn't implement Collection, it implements Map interface, so you cannot pass it to Collections.sort().

Collections.sort() takes a List as a parameter.
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
That's why I said: "On a side note.... since Collections.sort() takes a List then I assume if you need to sort a HashMap for example then you would have to use a method to grab all the keys or values as a List and pass that?"
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Originally posted by Lidija Sketa:
HashMap doesn't implement Collection, it implements Map interface, so you cannot pass it to Collections.sort().


But Set is a Collection, but still you cannot pass it to sort method. sort method only takes List not Collection. I wrote it here as I thought that you might be missing this which might hurt you in the exam...
Lidija Sketa
Greenhorn

Joined: Nov 12, 2008
Posts: 26
Good point, Ankit, thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting question
 
Similar Threads
How About Avatars?
Why does Groovy have this name?
java and c++ comparison
:mad: Bali Bombing!!!
Do you give money to beggars?