进阶day03-Collections里面的sort方法以及Comparable接口
This commit is contained in:
45
javaSE-day03/src/com/inmind/collections04/Demo14.java
Normal file
45
javaSE-day03/src/com/inmind/collections04/Demo14.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package com.inmind.collections04;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
/*
|
||||||
|
- public static <T> void sort(List<T> list):将集合中元素按照默认规则排序。
|
||||||
|
- public static <T> void sort(List<T> list,Comparator<? super T> ):将集合中元素按照指定规则排序。
|
||||||
|
|
||||||
|
注意:
|
||||||
|
1.要使用以上的排序api,那么List集合的泛型的类型必须实现Comparable接口.
|
||||||
|
2.当一个类实现了Comparable接口就表示该类具有了自然排序功能
|
||||||
|
|
||||||
|
*/
|
||||||
|
public class Demo14 {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//- public static <T> void sort(List<T> list):将集合中元素按照默认规则排序。
|
||||||
|
System.out.println("--------------对Integer集合排序:整数的升序----------------");
|
||||||
|
ArrayList<Integer> lists1 = new ArrayList<>();
|
||||||
|
Collections.addAll(lists1, 1, 44, 23, 56, 32, 66, 12);
|
||||||
|
System.out.println(lists1);
|
||||||
|
//调用排序功能
|
||||||
|
Collections.sort(lists1);
|
||||||
|
System.out.println(lists1);
|
||||||
|
|
||||||
|
System.out.println("--------------对String集合排序:根据字符串的字母逐个自然排序----------------");
|
||||||
|
ArrayList<String> lists2 = new ArrayList<>();
|
||||||
|
Collections.addAll(lists2, "ab","ba", "aa", "ca","abb","aba");
|
||||||
|
System.out.println(lists2);
|
||||||
|
//调用排序功能
|
||||||
|
Collections.sort(lists2);
|
||||||
|
System.out.println(lists2);
|
||||||
|
|
||||||
|
//注意:在实际开发中,我们的数据一般是对象,我们排序也应该按照对象去排序
|
||||||
|
System.out.println("-------------------对自定义对象集合排序----------------------");
|
||||||
|
ArrayList<Student> lists3 = new ArrayList<>();
|
||||||
|
lists3.add(new Student("张三2", 19));
|
||||||
|
lists3.add(new Student("张三1", 18));
|
||||||
|
lists3.add(new Student("张三3", 20));
|
||||||
|
System.out.println(lists3);
|
||||||
|
Collections.sort(lists3);
|
||||||
|
System.out.println(lists3);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
59
javaSE-day03/src/com/inmind/collections04/Student.java
Normal file
59
javaSE-day03/src/com/inmind/collections04/Student.java
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
package com.inmind.collections04;
|
||||||
|
|
||||||
|
public class Student implements Comparable<Student>{
|
||||||
|
String name;
|
||||||
|
int age;
|
||||||
|
int id;
|
||||||
|
|
||||||
|
public Student(String name, int age) {
|
||||||
|
this.name = name;
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
//如果没有重写equals方法,默认使用==比较地址来判断是否相同
|
||||||
|
//已经重写了equals方法,比较属性的内容
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
|
Student student = (Student) o;
|
||||||
|
return age == student.age && name.equals(student.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
//当前学生类的哈希值,跟内容有关,属性相同的学生对象的哈希值必定相同
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int result = name.hashCode();
|
||||||
|
result = 31 * result + age;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Student{" +
|
||||||
|
"name='" + name + '\'' +
|
||||||
|
", age=" + age +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
//将学生按年龄排序
|
||||||
|
// 升序:由小到大 降序:由大到小
|
||||||
|
/*
|
||||||
|
负整数,零或正整数,为该对象小于,等于或大于指定对象。
|
||||||
|
排序口诀:
|
||||||
|
我(this)-它(参数o):升序
|
||||||
|
它-我:降序
|
||||||
|
底层使用了接口回调.(框架中,匿名内部类作为参数)
|
||||||
|
接口回调:能够将我们的业务逻辑嵌入到源码中
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public int compareTo(Student o) {
|
||||||
|
// return this.age - o.age; 升序
|
||||||
|
// return o.age - this.age;//降序
|
||||||
|
//如果年龄不同,那么按照年龄升序排,如果年龄相同,按学号升序
|
||||||
|
if (this.age == o.age) {
|
||||||
|
return this.id - o.id;
|
||||||
|
} else {
|
||||||
|
return this.age = o.age;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user