/** * @author Lee Boynton * @author Arthur van Hoff * @author Martin Buchholz * @author Ulf Zibis * @see java.lang.Object#toString() * @see java.lang.StringBuffer * @see java.lang.StringBuilder * @see java.nio.charset.Charset * @since JDK1.0 */ public final class String implements java.io.Serializable, Comparable<String>, CharSequence { }
package com.jvm; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class T20160727 { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("ghi"); list.add("def"); list.add("abc"); System.out.println(list); sort(list); System.out.println(list); } public static <T extends Comparable<? super T>> void sort(List<T> list){ Collections.sort(list); } }
输出:
[ghi, def, abc]
[abc, def, ghi]
<? extends A>表示类型必须是A或者A的子类
<? super A>表示类型必须是A或者A的超类
下面是JDK String的定义
public final class String implements java.io.Serializable, Comparable<String>, CharSequence
这里满足String是Comparable<String>的子类,而Comparable中的T就是String本身