This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Whats the output??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Whats the output??? " Watch "Whats the output??? " New topic
Author

Whats the output???

himanshu kesarwani
Ranch Hand

Joined: Aug 27, 2008
Posts: 84
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 !!

SCJP 5
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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.


[My Blog]
All roads lead to JavaRanch
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
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.


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 823

Please use code tags while posting code on the forum


Regards, Sud.
SCJP 5 ScjpFAQ JLS
himanshu kesarwani
Ranch Hand

Joined: Aug 27, 2008
Posts: 84
kindly,explain clearly!!
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Whats the output???