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
CharSequenceinstances 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
CharSequenceis defined as follows. Consider aCharSequencecsof lengthlento be a sequence of char values,cs[0]tocs[len-1]. Supposekis 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
CharSequenceare equal; a negative integer if the firstCharSequenceis lexicographically less than the second; and a positive integer if the firstCharSequenceis lexicographically greater than the second.Since:
11
- StringBuilder - public final class StringBuilder extends AbstractStringBuilder implements java.io.Serializable, Comparable<StringBuilder>, CharSequence
API Note:
StringBuilderimplementsComparablebut does not overrideequals. Thus, the natural ordering ofStringBuilderis inconsistent with equals. Care should be exercised ifStringBuilderobjects are used as keys in aSortedMapor elements in aSortedSet. SeeComparable,SortedMap, orSortedSetfor more information.
    public int compareTo(StringBuilder another)Compares two
StringBuilderinstances 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:
compareToin interfaceComparable<StringBuilder>Parameters:
another - the
StringBuilderto be compared with.Returns:
the value 0 if this
StringBuildercontains the same character sequence as that of the argumentStringBuilder; a negative integer if thisStringBuilderis lexicographically less than theStringBuilderargument; and a positive integer if thisStringBuilderis lexicographically greater than theStringBuilderargument.Since:
11
- StringBuffer: implements Comparable<StringBuffer> - public final class StringBuffer extends AbstractStringBuilder implements java.io.Serializable, Comparable<StringBuffer>, CharSequence
API Note:
StringBufferimplementsComparablebut does not overrideequals. Thus, the natural ordering ofStringBufferis inconsistent with equals. Care should be exercised ifStringBufferobjects are used as keys in aSortedMapor elements in aSortedSet. SeeComparable,SortedMap, orSortedSetfor more information.
    public int compareTo(`StringBuffer` another)Compares two
StringBufferinstances 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:
compareToin interfaceComparable<StringBuffer>Implementation Note:
This method synchronizes on
this, the current object, but notStringBuffer anotherwith whichthis StringBufferis compared.Parameters:
another - the
StringBufferto be compared with.Returns:
the value 0 if this
StringBuffercontains the same character sequence as that of the argumentStringBuffer; a negative integer if thisStringBufferis lexicographically less than theStringBufferargument; and a positive integer if thisStringBufferis lexicographically greater than theStringBufferargument.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
 
-