Implementing the java.lang.Comparable Interface 3 – Object Comparison


Output from the program:

Click here to view code image

class VersionNumber
Unsorted array: [(3.49.1), (8.19.81), (2.48.28), (10.23.78), (9.1.1)]
Unsorted list:  [(3.49.1), (8.19.81), (2.48.28), (10.23.78), (9.1.1)]
Unsorted map: {(10.23.78)=5000, (3.49.1)=2000, (8.19.81)=3000, (9.1.1)=6000,
               (2.48.28)=4000}
Sorted set: [(2.48.28), (3.49.1), (8.19.81), (9.1.1), (10.23.78)]
Sorted map: {(2.48.28)=4000, (3.49.1)=2000, (8.19.81)=3000, (9.1.1)=6000,
             (10.23.78)=5000}
Sorted list:    [(2.48.28), (3.49.1), (8.19.81), (9.1.1), (10.23.78)]
Binary search in sorted list found key (9.1.1) at index: 3
Sorted array:   [(2.48.28), (3.49.1), (8.19.81), (9.1.1), (10.23.78)]
Binary search in sorted array found key (9.1.1) at index: 3

Example 14.9 is a client that uses the class VersionNumber from Example 14.8. Unlike previous attempts, the following code from Example 14.9 demonstrates that VersionNumber objects can now be maintained in sorted sets and maps. A sorted set is created at (4) based on the unsorted list vnoList, and a sorted map is created at (5) based on the unsorted map versionStatistics.

Click here to view code image

Set<VersionNumber> sortedSet = new TreeSet<>(vnoList);                    // (4)

Map<VersionNumber, Integer> sortedMap = new TreeMap<>(versionStatistics); // (5)

The output from executing this code shows that the elements in the set and the keys of the map are sorted in the natural ordering for version numbers:

Click here to view code image

Sorted set: [(2.48.28), (3.49.1), (8.19.81), (9.1.1), (10.23.78)]
Sorted map: {(2.48.28)=4000, (3.49.1)=2000, (8.19.81)=3000, (9.1.1)=6000,
             (10.23.78)=5000}

By default, the class TreeSet<E> relies on its elements to implement the equals() method and the compareTo() method. The output from the program in Example 14.9 shows that the TreeSet<VersionNumber> maintains its elements sorted in the natural ordering dictated by the compareTo() method. Analogously, the output from the program in Example 14.9 shows that the TreeMap<VersionNumber, Integer> maintains its entries sorted on the keys which are in the natural ordering dictated by the compareTo() method.

We can run generic operations on collections of version numbers. Utility methods provided by the Collections and Arrays classes in the java.util package are discussed in §15.11, p. 856, and §15.12, p. 864, respectively.

The following code sorts the elements in the list vnoList created at (2) in Example 14.9 according to their natural order:

Click here to view code image

Collections.sort(vnoList);                                              // (6)

The output from executing this code shows that the elements in the list are indeed sorted in ascending order:

Click here to view code image

Unsorted list:  [(3.49.1), (8.19.81), (2.48.28), (10.23.78), (9.1.1)]

Sorted list:    [(2.48.28), (3.49.1), (8.19.81), (9.1.1), (10.23.78)]

A binary search can be run on this sorted list to find the index of the version number (9.1.1) referenced by the reference searchKey at (7) in Example 14.9:

Click here to view code image

VersionNumber searchKey = new VersionNumber( 9, 1, 1);                  // (7)
int resultIndex = Collections.binarySearch(vnoList, searchKey);         // (8)

Natural ordering is assumed for the elements in the list. Executing the code prints the correct index of the search key in the sorted list:

Click here to view code image

Binary search in sorted list found key (9.1.1) at index: 3

Finally, the code in Example 14.9 sorts the elements in the array versions created at (1) according to their natural order:

Click here to view code image

Arrays.sort(versions);                                                 // (9)

The output from executing this code shows that the elements in the array are sorted as expected in ascending order:

Click here to view code image

Unsorted array: [(3.49.1), (8.19.81), (2.48.28), (10.23.78), (9.1.1)]

Sorted array:   [(2.48.28), (3.49.1), (8.19.81), (9.1.1), (10.23.78)]

We can run a binary search on this sorted list:

Click here to view code image

int resultIndex2 = Arrays.binarySearch(versions, searchKey);           // (10)

Again, natural ordering is assumed for the elements in the array. Executing the code prints the correct index of the search key in the sorted array:

Click here to view code image Binary search in sorted array found key (9.1.1) at index: 3

Leave a Reply

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