• 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

Whats the output???

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class flub
{
public static void main(string arg[])
{
List<String> x=new ArrayList<String>();
x.add("x"); x.add("xx"); x.add("Xx");

//insert code here

for(String s: x) system.out.println(s);
}
}


output:
xx
Xx
x


ANSWER IS:
Comparator c=Collection.reverseOrder();
Collection.sort(x,c);




GUYS i am getting just nothing that :
1. why comparator, and where is its overridden method and class .
2.what is reverseOrder(); its first time i am lookin at this .

please guys,kindly clarify how this output is coming?? and how all this Comparator is working here !!
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

2.what is reverseOrder(); its first time i am lookin at this


That's a typo in the answer. Look for Collections#reverseOrder in the API.
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The


returns a comparator which will use to reverse the data in reverseorder. So in the sort function the comparator object is passed to decide the manner
in which data need to be sorted.
 
Ranch Hand
Posts: 826
Eclipse IDE Oracle Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use code tags while posting code on the forum
 
himanshu kesarwani
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
kindly,explain clearly!!
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The


takes comparator as parameter. So each element is compared with the other using the overriden method for the interface and the collection is sorted in
that manner. Please check the implementation of reverseOrder method in jdk source.
 
reply
    Bookmark Topic Watch Topic
  • New Topic