练习

implements接口与extends接口:
extends可以实现父类,也可以调用父类初始化 this.parent()。而且会覆盖父类定义的变量或者函数。这样的好处是:架构师定义好接口,让工程师实现就可以了。整个项目开发效率和开发成本大大降低。
implements,实现父类,子类不可以覆盖父类的方法或者变量。即使子类定义与父类相同的变量或者函数,也会被父类取代掉。
比较id大小:
public int compareTo(Student o){
return this.id.compareTo(o.id);
}按姓名大小排序:
Collections.sort(studentList, new studentComparator());
System.out.println("-------------------排序前-------------------");
for(Student student:studentList){
System.out.println("学生:" + student.id + ":" student.name);
}总结:


How to do
Collections工具类,i.e sort 方法
comparable -- 默认的比较规则
comparator -- 临时的比较规则
Java集合框架:
Collection + Map
Collection : List -> ArrayList, Set -> HashSet
Map :HashMap <Key, Value>
comparable的具体实现.
List 什么的是已经定义好的子接口,你当然也可以自定义一个 interface接口



使用comparable接口进行排序首先让需要排序的实体类实现comparable接口并设置对于的泛型,之后实现comparable接口的compareTO接口返回类型是一个int类型,该方法会传入一个参数当这个参数与该实体中的属性比较相等时会返回0,如果小的话会返回一个正整数,如果大的话会返回一个负整数,String本身实现了comparable接口所以当集合泛型是String的时候就可以直接调用Conllections中的sort排序方法 ,当比较的属性类型是String的时候回按照字符串的比较规则进行比较
总结
Collection接口(增删查改方法):
List子接口:ArrayList实现类
Set子接口:HashSet实现类
Map接口(增删查改方法):Hashmap实现类
Collections工具类(排序方法)
Comparable接口
Comparator接口
Comparator临时比较规则实现后,如何应用在程序:
Collections.sort(studentList, new studentComparator());
使用Collections.sort()方法排序时,当括号中的List泛型没有继承comparable和comparator接口时,不能使用该方法,有以下两种做法:
1、默认比较规则:
需要把List的泛型,例如Student继承接口comparable,重写compareTo()方法,再使用Collections.sort(studentList)。
2、临时比较规则:
创建一个StudentComparator类继承Comparator接口,重写compare()方法,再使用Collections.sort(studentList,new StudentComparator)
public int compare(Student o1,Student o2){
return o1.name.compareTo(o2.name);
}
通过调用Collections.sort方法,添加刚刚编写的实现了Comparator接口的类作为参数,对Map进行排序。
在类的定义中加上implements Comparator<泛型>,使其支持Comparator接口。
及加上compare方法
另外还需要实现该接口的compareTo方法
在类的定义中加上implements Comparable <泛型>,使其支持Comarable接口
collection 与map家族 子接口 ->实现类
collection 家族成员
比较规则,规则。
java集合框架:Collection接口家族{List【ArrayList】,quue,Set[HashSet]},Map接口家族{HashMap}, Collections工具类(sort方法),Comparable接口,comparator接口。
List<Integer> il = new ArrayList<Integer>();
List<Student> st = new ArrayList<Student>();
String[] names = { "mike", "jack" ,"xiaoming","hong","like"};
Random random = new Random();
Integer k;
for (int i = 0; i < 5; i++) {
do {
k = random.nextInt(1000);
} while (il.contains(k));
il.add(k);
st.add(new Student(il.get(i) + "", names[i]));
System.out.println("成功添加了编号为"+il.get(i)+"的学生"+st.get(i).name);
}
System.out.println("-----排序前-----");
for (Student student : st) {
System.out.println("学生:" + student.id + ":" + student.name);
}
Collections.sort(st);
System.out.println("-----排序后-----");
for (Student student : st) {
System.out.println("学生:" + student.id + ":" + student.name);
}

Comparable---默认的比较规则
Comparator---临时的比较规则
sort调用
Api 说明
Comparator类
compareTo 代码示例
Comparable———默认的比较规则(自然条件)
Comparator————临时的比较规则(若改动需重写compare方法,改变return值)
122222
Comparable直接实现接口且重写compareTo();Comparator要创建实现新类且重写compare();
Compare返回值含义=0两个返回值相等,大于0对象1大于对象2,小于0对象1小于对象2,可以重写compareTo返回想比较的集合内的值
Collections.sort(List<T>list,Comparator<? super T> c)可以加或不加比较规则
compaerTo不能用于比较基本类型