简体   繁体   中英

can I ignore the use of the “comparable”

public class EmployeeSortTest {
    public static void main(final String[] args) {
        final Employee[] staff = new Employee[3];
        staff[0] = new Employee("Harry Hacker", 35000);
        staff[1] = new Employee("Carl Cracker", 75000);
        staff[2] = new Employee("Tony Tester", 38000);
        Arrays.sort(staff);
        for (final Employee e : staff) {
            System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
        }
    }
}

class Employee implements Comparable<Employee> {
    public Employee(final String n, final double s) {
        name = n;
        salary = s;
    }

    public String getName() {
        return name;
    }

    public double getSalary() {
        return salary;
    }

    public void raiseSalary(final double byPercent) {
        final double raise = salary * byPercent / 100;
        salary += raise;
    }

    @Override
    public int compareTo(final Employee other) {
        if (salary < other.salary) {
            return -1;
        }

        if (salary > other.salary) {
            return 1;
        }

        return 0;
    }

    private final String name;
    private double salary;
}

I am a beginner with Java, as I learn from the book written by Mr Cay S. Horstmann and his colleague, Core Java, Volume I: Fundamentals , I found something I can't quite understand. The example of "employeesorttest.java" on page 245.

What I can't get is the method compareTo . How does it change the output? That method only returns three numbers: 0, -1 and 1. It didn't change any position or the objects from staff . Also, if the code arrays.sort(staff) does work, why do we still need to use the interface?

I know that there must be some relations between both code.

The Comparable interface only tells other code where the element should be sorted relative to another element. It's up to the calling code to actually do something with that information.

Uses of Comparable include:

See the Object Ordering section in the Java Tutorial for more information.

The Comparable interface requires the definition of the compareTo method.

According to the Java docs,

int compareTo(T o)

Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.

If an object implements Comparable , then functions like Arrays.sort(...) can use it. Arrays.sort doesn't look at the objects in the Array like an Employee in your example, it only looks at it as a Comparable object. It only sees the compareTo method, but that's all that it needs to sort them.

This compare to java example is missing a few pieces that would make more sense. This method I will show you is called a bubble sort.

// A bubble sort for Strings. 
class SortString { 
    static String arr[] = { 
    "Now", "is", "the", "time", "for", "all", "good", "men", 
    "to", "come", "to", "the", "aid", "of", "their", "country" 
};

public static void main(String args[]) { 
    for(int j = 0; j < arr.length; j++) { 
        for(int i = j + 1; i < arr.length; i++) { 
          if(arr[i].compareTo(arr[j]) < 0) { 
          String t = arr[j]; 
          arr[j] = arr[i]; 
          arr[i] = t; 
        } 
     } 
    System.out.println(arr[j]); 
   } 
 } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM