Index Boge Anne Lise Grzech pierworodny 04 Dziecko miĹoĹci ustawa_o_transporcie_drogowym_wersja_obowiazujaca_od_01.04.2009_r. Leigh Lora Twelve Quickies of Christmas 04 Sarah's Seduction Jeffries Sabrina Taniec zmysĹĂłw Stare panny Swanlea 04 James Alan Gardner [League Of Peoples 04] Hunted Glen Cook Black Company 04 Silver_Spike 04. Oakley Natasha KrĂłlewski rod Ksiezniczka i magnat Glen Cook Garrett 04 Old Tin Sorrows Draka 04 Drakon, S. M. Stirling 04 Pora Na Milosc |
[ Pobierz całość w formacie PDF ] all of the fields in the class. And for good measure, throw in the toString() and hashCode() methods. 148 Understanding Comparable The source code for just such a class is shown in Listing 11-1. Listing 11-1: Creating a Comparable object. import java.util.*; public class Employee implements Comparable { String department, name; public Employee(String department, String name) { this.department = department; this.name = name; } public String getDepartment() { return department; } public String getName() { return name; } public String toString() { return "[dept=" + department + ",name=" + name + "]"; } public int compareTo(Object obj) { Employee emp = (Employee)obj; int deptComp = department.compareTo(emp.getDepartment()); return ((deptComp == 0) ? name.compareTo(emp.getName()) : deptComp); } public boolean equals(Object obj) { if (!(obj instanceof Employee)) { return false; } Employee emp = (Employee)obj; return department.equals(emp.getDepartment()) && name.equals(emp.getName()); } public int hashCode() { return 31*department.hashCode() + name.hashCode(); } } Tip When defining the compareTo() method, be sure not to overload the method by declaring it as having an argument of the specific object type you want to compare to. It won't be called naturally as it isn't part of the Comparable interface. Listing 11-2 demonstrates the usage of the comparable employee by creating a company with a bunch of employees. For simplicity's sake, names are held as one field of the form "Lastname, Firstname" to ensure that they are ordered properly in one field. Listing 11-2: Testing our Comparable object. 149 Comparator Basics import java.util.*; public class Company { public static void main (String args[]) { Employee emps[] = { new Employee("Finance", "Degree, Debbie"), new Employee("Finance", "Grade, Geri"), new Employee("Finance", "Extent, Ester"), new Employee("Engineering", "Measure, Mary"), new Employee("Engineering", "Amount, Anastasia"), new Employee("Engineering", "Ratio, Ringo"), new Employee("Sales", "Stint, Sarah"), new Employee("Sales", "Pitch, Paula"), new Employee("Support", "Rate, Rhoda"), }; Set set = new TreeSet(Arrays.asList(emps)); System.out.println(set); } } Running the program displays the sorted company roster on one really long line. Notice how the elements are sorted by department first and name second. [[dept=Engineering,name=Amount, Anastasia], [dept=Engineering,name=Measure, Mary], [dept=Engineering,name=Ratio, Ringo], [dept=Finance,name=Degree, Debbie], [dept=Finance,name=Extent, Ester], [dept=Finance,name=Grade, Geri], [dept=Sales,name=Pitch, Paula], [dept=Sales,name=Stint, Sarah], [dept=Support,name=Rate, Rhoda]] Comparator Basics While Comparable objects are able to compare multiple instances of themselves, a Comparator is a class that can compare two other objects. When you don't like the natural ordering of a class or your class doesn't implement Comparable, you can provide a custom ordering by creating a class that implements Comparator. Table 11-3 lists the methods of the Comparator interface. Table 11-3: Summary of the Comparator Interface VARIABLE/METHOD NAME VERSION DESCRIPTION compare() 1.2 Checks for ordering between two elements. equals() 1.2 Checks for equality with another Comparator. Understanding Comparator Of the two methods in the interface, you actually only have to implement one. The compare() method functions similarly to the compareTo() method, however, both objects to compare must be passed in as arguments: 150 Using Comparator public int compare(Object obj1, Object obj2) The equals() method has nothing to do with comparing elements for sorting. It is meant to compare the Comparator to another Comparator: public boolean equals(Object obj) As this method is already implemented in the Object class, your Comparator does not have to override the behavior. However, if you wish to provide a way for different comparators to be equal, override the method. It's possible that different implementations impose the same order on elements, but in different manners. There is one predefined Comparator already implemented for you. If you wish to sort elements in their reverse [ Pobierz całość w formacie PDF ] |
||||
Wszelkie Prawa Zastrzeżone! Lubię Cię. Bardzo. A jeszcze bardziej się cieszę, że mogę Cię lubić. Design by SZABLONY.maniak.pl. | |||||