目录
自定义数据类型放在数组中
Arrays.sort()方法
这种方法要用Arrays.sort()方法,方法完整表述:
根据指定比较器产生的顺序对指定对象数组进行排序。
static <T> void
sort( T[] a, Comparator<? super T> c)
解释:T[] a:
自定义数据类型的数组表示(a这个数组中的每一个元素都是自定义的数据类型,比如都是People类的对象);
Comparator<? super T> c:
Comparator的一个实现类对象,可以是Comparator的一个子类对象。
完整演示代码如下:
普通版代码
中规中矩的按照JDK的要求进行写,比较麻烦。
Person类:
public class Person implements Comparator<Person> {
private String name;
private int age;
public Person(){
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "姓名:"+name+"\t年龄:"+age+";";
}
/**
* 要实现排序功能就必须要重写Comparator接口中的compare方法
* @param o1
* @param o2
* @return
*/
@Override
public int compare(Person o1, Person o2) {
return o1.age- o2.age;
}
}
实现的主类:
/**
* 需求:
* 使用数组存储多个Person对象
* 对数组中的Person对象使用Arrays的sort方法通过年龄进行升序排序
*
*/
public class Main {
public static void main(String[] args) {
Person[] arr=new Person[]{
new Person("陆俊",16),
new Person("何凡",20),
new Person("奥特曼",100),
new Person("迪迦",10),
};
System.out.println("排序前的数组中的数据:");
for (Person per : arr) {
System.out.println(per);
}
Arrays.sort(arr,new Person());
//这样写也是可以的
// Arrays.sort(arr,arr[0]);
System.out.println("排序后的数组中的数据:");
for (Person per : arr) {
System.out.println(per);
}
}
}
运行结果:
优化版代码
使用了匿名内部类进行改写优化:
Person类:
public class Person {
public String name;
public int age;
public Person(){
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "姓名:"+name+"\t年龄:"+age+";";
}
}
运行主类:
/**
* 需求:
* 使用数组存储多个Person对象
* 对数组中的Person对象使用Arrays的sort方法通过年龄进行升序排序
*
* 使用匿名内部类改写!
*/
public class Main {
public static void main(String[] args) {
Person[] arr=new Person[]{
new Person("陆俊",16),
new Person("何凡",20),
new Person("奥特曼",100),
new Person("迪迦",10),
};
System.out.println("排序前的数组中的数据:");
for (Person per : arr) {
System.out.println(per);
}
Arrays.sort(arr, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.age- o2.age;
}
});
//这样写也是可以的
// Arrays.sort(arr,arr[0]);
System.out.println("排序后的数组中的数据:");
for (Person per : arr) {
System.out.println(per);
}
}
}
运行结果都是一样的,所以这里就不放图了哈!
高级版代码
使用Lambda表达式进行优化:
Person类和高级版的是一样的,这里就不重复了哈!
运行主类:
/**
* 需求:
* 使用数组存储多个Person对象
* 对数组中的Person对象使用Arrays的sort方法通过年龄进行升序排序
*
* 使用Lambda优化改写!
*/
public class Main {
public static void main(String[] args) {
Person[] arr=new Person[]{
new Person("陆俊",16),
new Person("何凡",20),
new Person("奥特曼",100),
new Person("迪迦",10),
};
System.out.println("排序前的数组中的数据:");
for (Person per : arr) {
System.out.println(per);
}
Arrays.sort(arr,(o1, o2) -> o1.age- o2.age);
//这样写也是可以的
// Arrays.sort(arr,arr[0]);
System.out.println("排序后的数组中的数据:");
for (Person per : arr) {
System.out.println(per);
}
}
}
自定义数据类型放在集合中
和自定义数据类型放在数组中的三种写法都是一样的,大同小异,这里就不多赘述了!(这里只演示一种写法)
Peolple类:
/**
* Collections.sort对People进行默认的升序排序
*/
public class People implements Comparable<People>{
private String name;
private int age;
public People() {
}
public People(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "姓名:"+name+"\t年龄:"+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 int compareTo(People o) {
return this.age-o.age;
}
}
运行主类:
/**
* 演示Collections工具类中的sort方法
*/
public class Main {
public static void main(String[] args) {
ArrayList<People> arr=new ArrayList<>();
arr.add(new People("迪迦",100));
arr.add(new People("赛罗",400));
arr.add(new People("人类",70));
arr.add(new People("外星人",20));
System.out.println("排序前的数组中的数据:");
for (People per : arr) {
System.out.println(per);
}
Collections.sort(arr);
System.out.println("排序后的数组中的数据:");
for (People per : arr) {
System.out.println(per);
}
}
}
运行截图:
总结
前者使用了Arrays工具类的方法,后者使用Collections工具类的方法!虽然两者要实现的接口和方法都不一样,但使用Lambda表达式就感觉是一样的了,也就是第三种写法。
Arrays.sort()方法中第二个参数要传入一个Comparator接口的实现类对象,这就必须要实现接口中的compare方法;
Collections.sort()方法中第二个参数要传入一个Comparable接口的实现类对象,这就必须要实现接口中的compareTo方法。
发现没有,两个长的炒鸡像,所以直接用Lambda表达式就不用区分了!哈哈!