Implementing the java.util.Comparator Interface – Object Comparison


14.5 Implementing the java.util.Comparator<E> Interface

The java.util.Comparator<E> interface is a functional interface and is designated as such with the @FunctionalInterface annotation in the Java SE API documentation— in other words, it is intended to be implemented by lambda expressions. Apart from its sole abstract method compare(), it defines a number of useful static and default methods which are listed at the end of this section. Several of these methods are illustrated throughout this chapter.

Precise control of ordering can be achieved by creating a customized comparator that imposes a specific total ordering on the elements. All comparators implement the Comparator<E> interface, providing implementation for its abstract method:

int compare(E o1, E o2)

The compare() method returns a negative integer, zero, or a positive integer if the first object is less than, equal to, or greater than the second object, according to the total ordering—that is, its contract is equivalent to that of the compareTo() method of the Comparable<E> interface. Since this method tests for equality, it is strongly recommended that its implementation does not contradict the semantics of the equals() method for the objects.

An alternative ordering to the default natural ordering can be specified by passing a Comparator to the constructor when the sorted set or map is created. The Collections and Arrays classes provide utility methods for sorting and searching, which also take a Comparator (§15.11, p. 856, and §15.12, p. 864).

Example 14.10 demonstrates the use of different comparators for strings. The program creates several empty sorted sets, using the TreeSet<E> class, where the comparator is passed to the constructor ((1b), (1c), (4)). Elements from the words array are added to each sorted set by the Collections.addAll() method, as at (6). A text representation of each sorted set is then printed, as at (7). The output shows the sort order in which the elements are maintained in the set.

Example 14.10 Natural Ordering and Total Orderings

Click here to view code image

import java.util.*;
public class ComparatorUsage {
  public static void main(String[] args) {
    String[] words = {“court”, “Stuart”, “report”, “Resort”,            // (1)
                      “assort”, “support”, “transport”, “distort”};
    // Choice of comparator.
    Set<String> strSet1 = new TreeSet<>();            // (1a) Natural ordering
    Set<String> strSet2 = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); // (1b)
    Set<String> strSet3 = new TreeSet<>(              // (1c) Rhyming ordering
      (String obj1, String obj2) -> {
        // Create reversed versions of the strings:                        (2)
        String reverseStr1 = new StringBuilder(obj1).reverse().toString();
        String reverseStr2 = new StringBuilder(obj2).reverse().toString();
        // Compare the reversed strings lexicographically.
        return reverseStr1.compareTo(reverseStr2);                      // (3)
      }
    );
    Set<String> strSet4 = new TreeSet<>(
        Comparator.comparingInt(String::length)       // (4) First length, then by
                  .thenComparing(Comparator.naturalOrder())// (5) natural ordering
    );
    // Add the elements from the words array to a set and print the set:
    Collections.addAll(strSet1, words);                                 // (6)
    System.out.println(“Natural order:\n” + strSet1);                   // (7)
    Collections.addAll(strSet2, words);
    System.out.println(“Case insensitive order:\n” + strSet2);
    Collections.addAll(strSet3, words);
    System.out.println(“Rhyming order:\n” + strSet3);
    Collections.addAll(strSet4, words);
    System.out.println(“Length, then natural order:\n” + strSet4);
  }
}

Output from the program:

Click here to view code image

Natural order:
[Resort, Stuart, assort, court, distort, report, support, transport]
Case insensitive order:
[assort, court, distort, report, Resort, Stuart, support, transport]
Rhyming order:
[Stuart, report, support, transport, Resort, assort, distort, court]
Length, then natural order:
[court, Resort, Stuart, assort, report, distort, support, transport]

Leave a Reply

Your email address will not be published. Required fields are marked *