Forums Register Login

Collection VS ArrayList.

+Pie Number of slices to send: Send
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.

Suresh
+Pie Number of slices to send: Send
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.

Dawn
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Hi Paul , Dawn,

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


Regards

Suresh.
The only cure for that is hours of television radiation. And this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2887 times.
Similar Threads
HashSet and sorting
for loop
Doubt in TreeSet
How is priority determined?-Collection Doubt
Interface Set's get() method>>>>
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 15, 2024 22:25:53.