[ Pobierz całość w formacie PDF ]

For instance, if our company set were sorted using the reverse comparator, the output would be as follows:
[[dept=Support,name=Rate, Rhoda], [dept=Sales,name=Stint, Sarah],
[dept=Sales,name=Pitch, Paula], [dept=Finance,name=Grade, Geri],
[dept=Finance,name=Extent, Ester], [dept=Finance,name=Degree, Debbie],
[dept=Engineering,name=Ratio, Ringo], [dept=Engineering,name=Measure, Mary],
[dept=Engineering,name=Amount, Anastasia]]
The set was created with the following code to Listing 11-2:
Set set = new TreeSet(Collections.reverseOrder());
set2.addAll(Arrays.asList(emps));
When creating a TreeSet with a custom Comparator, you can't use the copy constructor. You must manually
add each element to the set.
Using Comparator
To demonstrate writing a custom Comparator, imagine a new manager coming into the previous company
who likes to see employee names first and their department last. While the reverse order Comparator was able
to reverse the natural order, it can't reverse the sort order of the internal fields. For this to be done, you must
create your own Comparator. Listing 11-3 shows one such comparator.
Listing 11-3: Writing Your own Comparator.
import java.util.*;
public class EmpComparator implements Comparator {
public int compare(Object obj1, Object obj2) {
Employee emp1 = (Employee)obj1;
Employee emp2 = (Employee)obj2;
int nameComp = emp1.getName().compareTo(emp2.getName());
return ((nameComp == 0) ?
emp1.getDepartment().compareTo(emp2.getDepartment()) :
nameComp);
}
}
151
SortedSet
Now, when you use this comparator, the order of the elements would be as follows:
[[dept=Engineering,name=Amount, Anastasia], [dept=Finance,name=Degree,
Debbie], [dept=Finance,name=Extent, Ester], [dept=Finance,name=Grade, Geri],
[dept=Engineering,name=Measure, Mary], [dept=Sales,name=Pitch, Paula],
[dept=Support,name=Rate, Rhoda], [dept=Engineering,name=Ratio, Ringo],
[dept=Sales,name=Stint, Sarah]]
This is basically all there is to say with regards to comparators and comparables. The key thing to remember
when writing your own is to make sure that you don't accidentally use a non-unique sort field, like a date,
when you are using the Comparator with a SortedSet or SortedMap. If you do, only one of the elements will
be added to the collection.
Imagine adding a start date to the Employee class. If multiple employees started on the same date, their setting
would be the same. Now imagine creating a Comparator that allowed you to sort a group of employees on
their start date. When comparing employees with the same start date, compare() would produce a zero,
meaning they are equal, and the second employee (and beyond) would not be added to the set or tree. If you
run across this behavioral problem, you'll need to add a secondary field to the comparison to ensure instances
where equals() returns false results in compareTo() returning a non-zero value.
If you did have a misbehaving comparator, you could still use it to sort a List with Collections.sort(List,
Comparator) and an array of Object elements with Arrays.sort(Object[], Comparator). These methods are
described in Chapters 12 and 13, respectively.
SortedSet
The basis of maintaining order within the elements of a set is defined in the SortedSet interface. While set
elements by definition have no order, those concrete sets that also implement the SortedSet interface actually
do keep their elements ordered. As far as those concrete implementations provided with the Collections
Framework, only TreeSet implements the interface. This is shown in Figure 11-1.
The SortedSet interface has only six methods, which are listed in Table 11-4.
Figure 11-1: Class hierarchy of the SortedSet interface.
Table 11-4: Summary of the SortedSet Interface
152
Understanding SortedSet
VARIABLE/METHOD NAME VERSION DESCRIPTION
comparator() 1.2 Retrieves the comparator for the set.
first() 1.2 Retrieves the first element of the set.
headSet() 1.2 Retrieves a sub set from the beginning of the
entire set.
last() 1.2 Retrieves the last element of the set.
subSet() 1.2 Retrieves a sub set of the entire set.
tailSet() 1.2 Retrieves a sub set from the end of the entire
set.
Understanding SortedSet [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • grabaz.htw.pl