![]() A reversed version of a string is created using a string buffer, which is first reversed and then converted back to a string, as shown at (5). The compare() method at (4) first creates reversed versions of the strings passed as arguments. If we reverse the two strings, "report" and "court", the reversed string "troper" is lexicographically less than the reversed string "truoc".Ī rhyming order comparator is implemented by the RhymingStringComparator class in Example 11.5. According to the rhyming order, the string "report" is less than the string "court".Ĭomparing two strings according to the rhyming order is equivalent to reversing the strings and comparing the reversed strings lexicographically. Continuing backward in the two strings, the character 'o' in the first string is less than the character 'u' in the second string. For example, given the two strings "report" and "court", the last two characters in both the strings are the same. First the characters in the last position are compared, then those in the last but one position, and so on. ![]() In rhyming order, two strings are compared by examining their corresponding characters at each position in the two strings, starting with the characters in the last position. ![]() We can create a string comparator that enforces rhyming order on the strings. The output shows how the elements are maintained sorted in the set by this total order, which is a case-insensitive order. The comparator is passed as argument to the set constructor. This particular total order is used to maintain the program arguments sorted when the sorted set at (1b) is used. The String class provides a static field ( CASE_INSENSITIVE_ORDER) that denotes a comparator object with a compare() method that ignores the case when comparing strings lexicographically. If we wish to maintain the strings in a different ordering, we need to provide a customized comparator. The natural order is used to maintain the program arguments sorted lexicographically when the sorted set at (1a) is used. The compareTo() method defines the natural order for strings, which is lexicographical. The String class implements the Comparable interface, providing an implementation of the compareTo() method. The set is traversed according to the sorting order. The output shows the sorting order in which the elements are maintained in the set. A textual representation of the sorted set is then printed at (3). Each program argument is added to the sorted set in the loop at (2). The program creates an empty sorted set using the TreeSet class (discussed later in this section). 482).Įxample 11.5 demonstrates the use of different comparators for strings. The Collections and Arrays classes provide utility methods for sorting, which also take a Comparator (see Section 11.8, p. As elements in a sorted set, or as keys in a sorted map, the objects would be maintained in ascending order.Īn alternative ordering to the default natural order can be specified by passing a Comparator to the constructor when the sorted set or map is created. The natural order for objects of a numerical wrapper type is ascending order of the values of the corresponding numerical primitive type (see Section 10.3, p. A collection of strings sorted by natural order would be ordered in lexicographical order. Strings will be lexicographically maintained as elements in a sorted set or as keys in a sorted map that uses natural ordering. Note that the natural order for String objects (and Character objects) is lexicographical order (see Section 10.5, p. Objects implementing this interface can be used asĮlements in lists that are sorted manually using the Collections.sort() method Since this method tests for equality, it is recommended that its implementation does not contradict the semantics of the equals() method (see Section 11.7). It throws a ClassCastException if the reference value passed in the argument cannot be cast to the type of the current object. This method returns a negative integer, zero, or a positive integer if the current object is less than, equal to, or greater than the specified object, based on the natural order.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |