This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Collection VS ArrayList.

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul , Dawn,

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


Regards

Suresh.
 
Without deviation from the norm, progress is not possible - Zappa. Tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic