• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting question

 
Brian Legg
Ranch Hand
Posts: 488
  • Mark post as helpful
  • send pies
  • 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!
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
 
patrick avery
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Of course....

Thanks again Ankit!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Yes, but that would be ordered, not sorted
 
Lidija Sketa
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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
  • 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: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Good point, Ankit, thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic