- 泛型的好处
- 泛型类:就是类的后面存在<>
- 泛型以及泛型类的格式
特点:
- 泛型类确定泛型的时机创建对象的时候,如果不确定,系统确定成Object
- 泛型要慎用,如果确定类型不要再用泛型,增加烦恼。
- 泛型类的泛型在静态方法中不可以使用的,因为泛型类在创建对象的时候,而静态方法的调用不用创建对象
public class Test02FanXingLei {
public static void main(String[] args) {
Box box1 = new Box<>();
box1.setElement("小狗狗");
String element1 = box1.getElement();
System.out.println(element1);//小狗狗
Box box2 = new Box<>();
box2.setElement(19);
Integer element2 = box2.getElement();
System.out.println(element2);//19
}
}
//创建一个泛型类
class Box{
//创建成员变量,E就是成员变量的类型
private E element;
public E getElement() {
return element;
}
public void setElement(E element) {
this.element = element;
}
- 泛型方法
public class Test05 {
public static void main(String[] args) {
ArrayList list1 = addElement(new ArrayList(), "a", "b", "c", "d");
System.out.println(list1);//[a, b, c, d]
ArrayList list2 = addElement(new ArrayList(), 1,2,3,4);
System.out.println(list2);//[1, 2, 3, 4]
}
public static ArrayList addElement(ArrayList list,E t1,E t2,E t3,E t4){
list.add(t1);
list.add(t2);
list.add(t3);
list.add(t4);
return list;
}
}
- 泛型接口
- 通配符
TreeSet 集合自排序的方式
- 有值特性的元素直接可以升序排序(整型,浮点型)
- 字符串类型的元素会按照首字符的编号排序
- 对于自定义的引用数据类型,TreeSet 默认无法排序,执行的时候直接报错,因为人家不知道排序规则
自定义的引用数据类型的排序实现
– 对于自定义的引用数据类型,TreeSet 默认无法排序
– 所以我们需要定制排序的大小规则,方案有两种
- 直接为对象的类实现比较器规则接口 Comparable,重写比较方法
- 直接为集合设置比较器Comparator对象,重写比较方法
- 如果类和集合都存在大小规则,默认使用集合自带的规则进行大小排序!
自然排序
public class Test12TreeSet {
public static void main(String[] args) {
TreeSet ts = new TreeSet<>();
ts.add(new Student("aaa",18,178,80));
ts.add(new Student("ddd",20,178,70));
ts.add(new Student("bbb",16,169,80));
ts.add(new Student("ccc",20,178,70));
for (Student t : ts) {
System.out.println(t);
//输出
}
}
}
//自然排序,实现接口,重写方法
class Student implements Comparable{
private String name;
private int age;
private int height;
private int weight;
public Student() {
}
public Student(String name, int age, int height, int weight) {
this.name = name;
this.age = age;
this.height = height;
this.weight = weight;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + ''' +
", age=" + age +
", height=" + height +
", weight=" + weight +
'}';
}
@Override
public int compareTo(Student o) {
//this(正在添加的对象) 0(集合中有的对象)
int result = this.age - o.age;//正数
result = result==0?this.height -o.height:result;
result = result==0?this.weight-o.weight:result;
return result;
}
}
比较器排序
public class Test13BiJiaoQi {
public static void main(String[] args) {
//使用比较器排序
TreeSet ts = new TreeSet<>(new Comparator() {
@Override
public int compare(Teacher o1, Teacher o2) {
//01:表示现在要存入的那个元素
//02:表示已经存入到集合中的元素
int result = o1.getAge() - o2.getAge();
result = result == 0 ? o1.getName().compareTo(o2.getName()) : result;
return result;
}
});
//创建Teacher对象
ts.add(new Teacher("wang",23));
ts.add(new Teacher("li",24));
ts.add(new Teacher("zhao",25));
ts.add(new Teacher("jin",25));
for (Teacher t : ts) {
System.out.println(t);
//输出
}
}
}
//编写Teacher类
class Teacher{
private String name;
private int age;
public Teacher() {
}
public Teacher(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Teacher{" +
"name='" + name + ''' +
", age=" + age +
'}';
}
}



