A mutable sequence of characters.(可变序列序列),but with no guarantee of synchronization(但是无法保证同步),used by a single thread,it will be faster under most implementation.
### 1. java.lang.Comparable的使用
o:specified object
return:
a negative integer less than
zero equal to
a positive integer greater than
public int compareTo(T o);
查看String的源码发现实现此接口,String的比大小规则:两个字符串均从第一个字符起,比较字符的Unicode Value的大小
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
publicintcompareTo(String anotherString){ int len1 = value.length; int len2 = anotherString.value.length; int lim = Math.min(len1, len2); char v1[] = value; char v2[] = anotherString.value;
int k = 0; while (k < lim) { char c1 = v1[k]; char c2 = v2[k]; if (c1 != c2) { return c1 - c2; } k++; } return len1 - len2; }
### 2. 其他地方的使用
Lists (and arrays) of objects that implement this interface can be sorted automatically by Collection#sort(list) Collections.sort And Arrays.srot(Object[]) Arrays.sort.
这里提供集合对象的排序可以自动使用2个实现。
Collections.class
@SuppressWarnings({"unchecked", "rawtypes"}) defaultvoidsort(Comparator<? super E> c) { Object[] a = this.toArray(); Arrays.sort(a, (Comparator) c); ListIterator<E> i = this.listIterator(); for (Object e : a) { i.next(); i.set((E) e); } }
Arrays.class
```
public static <T> void sort(T[] a, Comparator<? super T> c) {
if (c == null) {
sort(a);
} else {
if (LegacyMergeSort.userRequested)
legacyMergeSort(a, c);
else
TimSort.sort(a, 0, a.length, c, null, 0, 0);
}
}
```
### 3.CharSequence
3.1简介
<tt>CharSequence</tt> is a readable sequence of <code>char</code> values. This interface provides uniform, read-only access to many different kinds of <code>char</code> sequences.
此接口提供对于char的可读序列,可以用来提供统一,仅读 对于各种各样的char序列(String,StringBuilder,StringBuffer)