1.语法:<T,...> T称为类型占位符,表示一种引用类型;泛型只能使用引用类型;不同泛型类型对象之间不能相互赋值。
2.泛型类:public class Demo8 <T> {}; 泛型接口:public interface MyInterface<T> {} (确定接口时就确定泛型类型的) 实现泛型接口:public class MyInterfaceImpl implements MyInterface<String> {}; (确定接口时未确定泛型类型):public class MyInterfaceImpl2<T> implements MyInterface<T>{} 泛型方法:public <T> void show(T t) { System.out.println(t);} 泛型集合 ArrayList<T> arr = new AarrayList<>;
3.TreeSet
使用TreeSet保存数据 * 存储结构:红黑树 * 要求:元素必须要实现Comparable接口.compareTo返回值为0,则认为是重复的 TreeSet集合的使用 * Comparator:实现定制比较(比较器)//无需再去Person类里实现接口 * Comparable:可比较的 TreeSet<Person > person = new TreeSet<>(new Comparator<Person>(){}