Summary
Add a static method to CharSequence to allow comparison between two CharSequence implementations; Add support for the Comparable interface to StringBuilder and StringBuffer.
Problem
A CharSequence is semantically comparable. It is expected therefore its implementations, specifically, StringBuffer and StringBuilder are comparable. String is already comparable.
Solution
Add a static method to CharSequence
It would be desirable for CharSequence to implement Comparable. Unfortunately since String that implements CharSequence already implements Comparable<String>, it is not feasible to change CharSequence to implement Comparable<CharSequence>. For more detailed discussion, please refer to the comment section of the original enhancement request.
The alternative solution therefore is to introduce a static method to CharSequence:
static int compare(CharSequence cs1, CharSequence cs2)
This compare method will allow the comparison between CharSequence implementations such as String, StringBuilder and StringBuffer.
Implement Comparable for StringBuilder and StringBuffer
The StringBuilder and StringBuffer shall implement Comparable<StringBuilder> and Comparable<StringBuffer> respectively, the same way as String implements Comparable<String>. The addition will extend the functionality to allow a StringBuilder to StringBuilder, or StringBuffer to StringBuffer comparison.
Specification
- CharSequence: change in general description (the wording changes are in bold)
-
This interface does not refine the general contracts of the equals and hashCode methods. The result of comparing two objects that implement CharSequence is therefore, in general, undefined. Each object may be implemented by a different class, and there is no guarantee that each class will be capable of testing its instances for equality with those of the other. It is therefore inappropriate to use arbitrary CharSequence instances as elements in a set or as keys in a map
+
This interface does not refine the general contracts of the equals and hashCode methods. The result of testing two objects that implement CharSequence for equality is therefore, in general, undefined. Each object may be implemented by a different class, and there is no guarantee that each class will be capable of testing its instances for equality with those of the other. It is therefore inappropriate to use arbitrary CharSequence instances as elements in a set or as keys in a map
CharSequence: add a static method
static int compare(CharSequence cs1, CharSequence cs2)
Compares two
CharSequence
instances lexicographically. Returns a negative value, zero, or a positive value if the first sequence is lexicographically less than, equal to, or greater than the second, respectively.The lexicographical ordering of
CharSequence
is defined as follows. Consider aCharSequence
cs
of lengthlen
to be a sequence of char values,cs[0]
tocs[len-1]
. Supposek
is the lowest index at which the corresponding char values from each sequence differ. The lexicographic ordering of the sequences is determined by a numeric comparison of the char valuescs1[k]
withcs2[k]
. If there is no such indexk
, the shorter sequence is considered lexicographically less than the other. If the sequences have the same length, the sequences are considered lexicographically equal.Parameters:
cs1 - the first
CharSequence
.
cs2 - the secondCharSequence
.Returns:
the value 0 if the two
CharSequence
are equal; a negative integer if the firstCharSequence
is lexicographically less than the second; and a positive integer if the firstCharSequence
is lexicographically greater than the second.Since:
11
StringBuilder
public final class StringBuilder extends AbstractStringBuilder implements java.io.Serializable, Comparable<StringBuilder>, CharSequence
API Note:
StringBuilder
implementsComparable
but does not overrideequals
. Thus, the natural ordering ofStringBuilder
is inconsistent with equals. Care should be exercised ifStringBuilder
objects are used as keys in aSortedMap
or elements in aSortedSet
. SeeComparable
,SortedMap
, orSortedSet
for more information.
public int compareTo(StringBuilder another)
Compares two
StringBuilder
instances lexicographically. This method follows the same rules for lexicographical comparison as defined in theCharSequence.compare(this, another)
method.For finer-grained, locale-sensitive String comparison, refer to
Collator
.Specified by:
compareTo
in interfaceComparable<StringBuilder>
Parameters:
another - the
StringBuilder
to be compared with.Returns:
the value 0 if this
StringBuilder
contains the same character sequence as that of the argumentStringBuilder
; a negative integer if thisStringBuilder
is lexicographically less than theStringBuilder
argument; and a positive integer if thisStringBuilder
is lexicographically greater than theStringBuilder
argument.Since:
11
StringBuffer: implements Comparable<StringBuffer>
public final class StringBuffer extends AbstractStringBuilder implements java.io.Serializable, Comparable<StringBuffer>, CharSequence
API Note:
StringBuffer
implementsComparable
but does not overrideequals
. Thus, the natural ordering ofStringBuffer
is inconsistent with equals. Care should be exercised ifStringBuffer
objects are used as keys in aSortedMap
or elements in aSortedSet
. SeeComparable
,SortedMap
, orSortedSet
for more information.
public int compareTo(`StringBuffer` another)
Compares two
StringBuffer
instances lexicographically. This method follows the same rules for lexicographical comparison as defined in theCharSequence.compare(this, another)
method.For finer-grained, locale-sensitive String comparison, refer to
Collator
.Specified by:
compareTo
in interfaceComparable<StringBuffer>
Implementation Note:
This method synchronizes on
this
, the current object, but notStringBuffer another
with whichthis StringBuffer
is compared.Parameters:
another - the
StringBuffer
to be compared with.Returns:
the value 0 if this
StringBuffer
contains the same character sequence as that of the argumentStringBuffer
; a negative integer if thisStringBuffer
is lexicographically less than theStringBuffer
argument; and a positive integer if thisStringBuffer
is lexicographically greater than theStringBuffer
argument.Since:
11
specdiffs attached. Below is a convenient link:
http://cr.openjdk.java.net/~joehw/jdk11/8137326/specdiff/overview-summary.html
- csr of
-
JDK-8137326 Methods for comparing CharSequence, StringBuilder, and StringBuffer
- Resolved