File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Collection   VS  ArrayList. 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 » Java » Beginning Java
Bookmark "Collection   VS  ArrayList." Watch "Collection   VS  ArrayList." New topic

Collection VS ArrayList.

Suresh Rajadurai
Ranch Hand

Joined: Feb 22, 2007
Posts: 58
Hi Folks,

I compiled the following 2 java programs, the first one gives compilation error, the 2nd one compiles fine.
The first program, the compiler does not recognize the command "Collections.sort(s)". Could anyone please throw some light on it please.

Program 1:

Program 2:

Thanks in advance.

Dawn Charangat
Ranch Hand

Joined: Apr 26, 2007
Posts: 249
Hello Suresh..,

The sort() method in Collections utility class takes in only objects of the type "List". Just since
the List interface extends from Collection interface doesn't mean you can directly cast it into the method.
May be this could help solve your confusion.

Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
The reason the sort method is applied to a List and not just any Collection is because not all Collection implementations are ordered.

Lists are ordered, that iteration over the elements is guaranteed to return them in a consistent order.

Sets are unordered, that is iteration over the elements is not guaranteed to return them in a consistent order.
Suresh Rajadurai
Ranch Hand

Joined: Feb 22, 2007
Posts: 58
Hi Paul , Dawn,

Thank you so much for the beautiful explanation. Much appreciated.


It is sorta covered in the JavaRanch Style Guide.
subject: Collection VS ArrayList.
Similar Threads
Doubt in TreeSet
Interface Set's get() method>>>>
HashSet and sorting
for loop
How is priority determined?-Collection Doubt