Here is my Code
class ComparableTest
{
public static void main(String[] args)
{
BOX[] box = new BOX[5];
box[0] = new BOX(10,8,6);
box[1] = new BOX(5,10,5);
box[2] = new BOX(8,8,8);
box[3] = new BOX(10,20,30);
box[4] = new BOX(1,2,3);
Arrays.sort(box);
for(int i=0;i<box.length;i++)
System.out.println(box[i]);
}
}
Also i have a class BOX that implements Comparable. Now i have a few question that i would like you all to help me out with.
1.Are the methods declared in comparable interface,system defined, as in can i have any method in the comparable, or it has to be compareTo
only?
2.I did not provide the implementation of Arrays.sort method, how does it sort my elements then?
3.When i use Comparator
instead of comparable
, then I use:
class comparatorTest
{
public static void main(String args[])
{
Student[] students = new Student[5];
Student[0] = new Student(“John”,”2000A1Ps234”,23,”Pilani”);
Student[1] = new Student(“Meera”,”2001A1Ps234”,23,”Pilani”);
Student[2] = new Student(“Kamal”,”2001A1Ps344”,23,”Pilani”);
Student[3] = new Student(“Ram”,”2000A2Ps644”,23,”Pilani”);
Student[4] = new Student(“Sham”,”2000A7Ps543”,23,”Pilani”);
// Sort By Name
Comparator c1 = new studentbyname();
Arrays.sort(students,c1);
for(int i=0;i<students.length;i++)
System.out.println(students[i]);
}
}
//In the above code, studentbyname implements comparator
, but box stil implements 开发者_开发问答comparable
.i.e
class studentbyname implements comparator
{
public int compare(Object o1,Object o2)
{
Student s1 = (Student) o1;
Student s2 = (Student) o2;
return s1.getName().compareTo(s2.getName());
}
}
Now i am doing Arrays.sort(students,c1)
, why am i passing c1 now?
In order to meet the Comparable contract, you must have at least the compareTo method. You may have as many addition methods in your class as you would like.
It sorts the elements in the natural order based on the Comparable interface. So sort is calling compareTo between the elements to see in which order to place them.
Providing a Comparator to the sort method allows sort to order elements that either (a) don't implement Comparable or (b) where you want to order them in some other order than the "natural order" as defined by the class's Comparable implementation.
When you pass a Comparator to sort, it calls the Comparator's compare method rather than the elements' compareTo method (if implemented).
see What is an interface
see Comparator
see Comparable
- You can define as many methods as you want in a
Comparable
, as long as you implementcompareTo
. This method can be used in many situations where the class is checked for comparation. For example, when inserting instances into an orderedTreeSet
.compareTo
provides a general ordering rule for all instances of the class. Arrays.sort
orders the array in the natural order of its elements. That is, usingcompareTo
.- You can use
Comparator
to define a custom ordering rule. Like when you have a table you can sort by any of its columns. You could define aComparator
for each of its columns, and you could still have a class-inherent ordering rule (as in related to the reality the Class represents) defined in the class'compareTo
.
Implementing Comparable obligates you to provide an implementation for compareTo().
All elements in the Object array passed to the Arrays.sort(Object[]) method must implement Comparable. If you want to use a Comparator instead, you have to use an Arrays.sort() method that takes a Comparator
as a parameter. The method you use in your example above takes a Comparator
as the second parameter - hence the need to provide c1
in the method call.
精彩评论