Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Whats the output???

 
himanshu kesarwani
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • 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 !!
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • 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.
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • 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.
 
sudipto shekhar
Ranch Hand
Posts: 826
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
kindly,explain clearly!!
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic