• 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

Sorting question

 
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that Arrays.sort only handles array objects (arrays of primitives or objects) while Collections sort only handles List objects.
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
*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
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course....

Thanks again Ankit!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are also LinkedHashMap and LinkedHashSet, which keep the elements in the order that you put them into the map or set.
 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but that would be ordered, not sorted
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 488
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good point, Ankit, thanks.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic