This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about Collections.sort() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about Collections.sort()" Watch "Question about Collections.sort()" New topic

Question about Collections.sort()

MuraliH Krishna

Joined: Jan 19, 2008
Posts: 9
can any one kindly explain what is the mistake I am doing in the following code. Why it is not compiling.

import java.util.*;
public class CollectionTest1 {
public static void main(String[] a)
Set<animal> s = new HashSet<animal>();
s.add(new animal("aa"));
s.add(new animal("ab"));
s.add(new animal("ac"));
s.add(new animal("ad"));

animal[] ar = (animal[])s.toArray();



class animal implements Comparable<animal>
String i;
animal(String i)
this.i = i;
public String getI()
return i;
public int compareTo(animal a1) {
return i.compareTo(a1.getI());

Thanks in advance.
Henry Wong

Joined: Sep 28, 2004
Posts: 20025

Why it is not compiling.

It would help if you tell us the compiler error that you are getting.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Erica Barbato

Joined: Oct 01, 2007
Posts: 8
Hi, Collections.sort needs a List as parameter,but you are using a Set
[ January 20, 2008: Message edited by: erishine ]

I agree. Here's the link:
subject: Question about Collections.sort()
jQuery in Action, 3rd edition